0
봄에 익숙하지 않습니다. 그래서 나는 아래의 코드에 어떤 문제점이 있는지를 알 수 없었다. 두 클래스가 있습니다. @Bean Channel을 정의하고 있으며 다음 클래스에서 해당 빈을 autowiring하고 있습니다. autowiring에 주석을 사용하고 있습니다. "System.out.println (externalChannel.toString());"을 시도하면 null 값을 받고 예외 널 포인터 예외가 발생합니다.자동 배선을 통해 @bean 값을 다른 클래스로 전달할 수 없습니다.
@Configuration
public class MessagingConfig
{
@Value("${service.name}")
private String queueName; // NOSONAR
@Value("${${user}")
private String schema;
@Value("${Owner}")
private String owner;
@Bean
public Channel externalChannel()
{
EventsProvider eventsProvider = new EventsProvider();
eventsProvider.setOwner(owner);
System.out.println("-------Cost And Events Channel-------"+eventsProvider.getEventsChannel());
return eventsProvider.getEventsChannel();
}
}
그리고 또 다른 클래스는
@Component
@LoggedService
@Monitored(useActualType = true)
public class MessagePublish {
@Autowired
private MessagingService messageService;
@Autowired
private Channel externalChannel;
public void publishTPSMessage(SourceTransaction sourceTransaction)
{
TPSEvent event = new TPSEvent(ContextHolder.getContextId(), new Date(), GuidUtils.generateGuid(),sourceTransaction);
Message<TPSEvent> message = new Message<TPSEvent>(event);
message.getHeader().setMessageType(TPSEvent.class.getName());
message.getHeader().setPayloadEncodingType(SystemCodecs.XML.name());
System.out.println(message.toString());
System.out.println(externalChannel.toString());
messageService.publish(externalChannel, message);
}
}
추가 정보입니다
public Channel getEventsChannel() {
return Channel.builder()
.setName("necessarySources")
.setConnectionName("defaultConnection")
.setType(ChannelType.Topic)
.setConnectionData(AqSqlConnectionData.buildString(this.owner, "Safi"))
.setSubscriberNames(new String[]{"Safi_Autowire"})
.build();
}
메인 클래스
public class TPSHandler {
public static void main(String[] args) {
BatchExport batchExportBean=getBatchExportASJAXBElement();
System.out.println(" batchExportBean Trans Description : " + batchExportBean.getDueDate());
for(Pay pay :batchExportBean.getPay()) {
SourceTransaction sourceTransaction=mapPayBatchToSourceTransaction(pay,batchExportBean);
String sourceTraString = getSourceTransactionASXML(sourceTransaction);
System.out.println(" sourceTraString : \n" + sourceTraString);
MessagePublish messagePublish= new MessagePublish();
//SourceTransaction sourceTransaction= new SourceTransaction();
messagePublish.publishTPSMessage(sourceTransaction);
}
}
}
내 servlet.xml에
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:config-properties.xml" />
<import resource="classpath:datasource.xml" />
<import resource="classpath:transaction-manager.xml" />
<import resource="classpath:jmx.xml" />
<context:component-scan base-package="com.imosAdapter" />
<bean class="com.oasis.services.messaging.config.MessagingServicesConfig" />
<bean class="com.oasis.services.messaging.config.DefaultMessagingConnectorsConfig" />
<bean class="com.oasis.services.messaging.config.LoaderConfig" />
<context:annotation-config />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/config/oasis-component.properties</value>
</property>
</bean>
</beans>
예외가 발생합니다 :
Exception in thread "main" java.lang.NullPointerException
at com.imosAdapter.tps.events.MessagePublish.publishTPSMessage(MessagePublish.java:46)
at com.imosAdapter.tps.TPSHandler.main(TPSHandler.java:64)
여기에 문제가 무엇입니까? 아무도 이걸 도와 줄 수 있니? 정확히 내가 여기서 누락 된거야?
메인 클래스보다 높은 레벨의 MessagingConfig 파일입니까? '@ ComponentScan' 어노테이션을 사용하면 해당 어노테이션이있는 클래스의 동일하거나 낮은 레벨에있는 패키지 만 스캔합니다. 필자가 설명한 것처럼'@Import (MessagingConfig.class)'를 사용하거나'@ ComponentScan'이 볼 수있는 곳에'MessagingConfig' 파일을 넣으십시오. (추신 : 스프링 부트를 사용하는 경우 기본값으로 @ @ ComponentScan이 사용됩니다.) –
@ er-han ComponentScan에서 읽을 수있는 MessagingConfig를 추가하려고했습니다. 하지만 여전히 null 포인터 예외가 발생하고 있습니다. 나는 스프링 부트를 사용하지 않고있다. –
@MohamedSafi는 오타이다 :'@Value ("$ {$ {user}")'? –