0

我是春天的新手。所以我无法弄清楚下面的代码中有什么问题。我有两节课。我正在定义一个 @Bean Channel 并在下一个类中自动装配该 bean。我正在使用注释进行自动装配。当我尝试“System.out.println(externalChannel.toString());” 我得到空值并且抛出异常空指针异常。

@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)

这里有什么问题?有人可以帮我吗?我到底在这里想念什么?

4

1 回答 1

0

当您不创建 servlet/作为服务器运行时,您需要在应用程序开始时初始化 Spring 上下文。

请参阅在独立应用程序中使用 Spring

于 2017-12-01T08:09:39.273 回答