Couldn't pass the @bean value to another class through Autowiring

350 Views Asked by At

I am new to spring. So I couldn't figure out what issue is there in the below code. I have two classes. I am defining a @Bean Channel and am autowiring that bean in the next class. I am using annotation for autowiring. When I try " System.out.println(externalChannel.toString());" am getting null values and the exception null pointer exception is thrown.

@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();
    }    
}

And Another class is

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

More Info

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();
    }

Main Class

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);
        }
    }   
}

My 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>

The exception thrown:

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)

What is the issue over here? Can anybody help me with this? what exactly I am missing here?

1

There are 1 best solutions below

0
On

You need to initialize a Spring context at the start of your application when you do not create a servlet / run as a Server.

See Using Spring in a standalone application