java - Spring-Boot @Autowired in main class is getting null -


i want connect sonic broker topic , listen incoming xml message. did below;

application.java

@springbootapplication @componentscan({"com.mainpack", "com.msgpack.jms"}) @enablejms public class application extends springbootservletinitializer {  @autowired private jmstopiclistener jmstopiclistener;  @override protected springapplicationbuilder configure(springapplicationbuilder application) {     return application.sources(application.class); }  @override public void onstartup(final servletcontext servletcontext) throws servletexception {     try {         logservice.info(application.class.getname(), "starting service...");         super.onstartup(servletcontext);         jmstopiclistener.listenmessage();         logservice.info(application.class.getname(), "service started");     } catch (exception ex) {         logservice.error(this.getclass().getname(), ex);     } }  public static void main(string[] args) {     applicationcontext context = springapplication.run(application.class, args);     logservice.info(application.class.getname(), "service started...");  } } 

jmstopiclistener.java

@component public class jmstopiclistener {  @autowired private applicationproperties properties;  @autowired private msglistener msglistener;  public void listenmessage() {     topicconnectionfactory factory;     topicconnection connection = null;     logservice.info(this.getclass().getname(), "registering broker connection");     try {         factory = new progress.message.jclient.topicconnectionfactory(properties.getbrokerurl());         connection = factory.createtopicconnection(properties.getusername(), properties.getpass());          javax.jms.topicsession subsession = (topicsession) connection.createtopicsession(false, session.auto_acknowledge);          javax.jms.topic topic = subsession.createtopic(properties.gettopicname());         messageconsumer subscriber = subsession.createsubscriber(topic);         subscriber.setmessagelistener(msglistener);         connection.start();         logservice.info(this.getclass().getname(), "broker connected");     } catch (exception ex) {         logservice.error(this.getclass().getname(), ex);     }  } } 

msglistener.java

@component public class msglistener implements messagelistener {  @override public void onmessage(message msg) {     if (msg instanceof xmlmessage) {         try {             xmlmessage m = (xmlmessage) msg;             if (m.gettext().contains("applications")) {                 logservice.info(this.getclass().getname(), "recieved applications message");             } else {                 logservice.info(this.getclass().getname(), "recieved message not contain applications tag");             }         } catch (exception ex) {             logservice.info(this.getclass().getname(), "exception: " + ex.getmessage());         }     }  } } 

when, run code nullpointer @ line jmstopiclistener.listenmessage() in application.java.

what mistake have made here? there way can improve (i mean work done in less code maybe)?.

note: com.mainpack have classes application.java , applicationprop.java com.msgpack.jms have jmstopiclistener.java , msglistner.java

error logger:

error [2015-07-14 14:34:52] [com.mainpack.application] [localhost-startstop-1] - [exception: ]java.lang.nullpointerexception     @ com.mainpack.application.onstartup(application.java:33)     @ org.springframework.web.springservletcontainerinitializer.onstartup(springservletcontainerinitializer.java:175)     @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5156)     @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150)     @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:725)     @ org.apache.catalina.core.containerbase.addchild(containerbase.java:701)     @ org.apache.catalina.core.standardhost.addchild(standardhost.java:717)     @ org.apache.catalina.startup.hostconfig.deploywar(hostconfig.java:945)     @ org.apache.catalina.startup.hostconfig$deploywar.run(hostconfig.java:1768)     @ java.util.concurrent.executors$runnableadapter.call(unknown source)     @ java.util.concurrent.futuretask.run(unknown source)     @ java.util.concurrent.threadpoolexecutor.runworker(unknown source)     @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source)     @ java.lang.thread.run(unknown source) 

onstartup called servlet container in application's lifecycle , called on instance of class created servlet container, not spring boot. why jmstopiclistener null.

rather overriding onstartup use method annotated @postconstruct. called spring once it's created instance of application and injected dependencies:

@springbootapplication @componentscan({"com.mainpack", "com.msgpack.jms"}) @enablejms public class application extends springbootservletinitializer {      @autowired     private jmstopiclistener jmstopiclistener;      @override     protected springapplicationbuilder configure(springapplicationbuilder application) {         return application.sources(application.class);     }      @postconstruct     public void listen() {          jmstopiclistener.listenmessage();     }      public static void main(string[] args) {         applicationcontext context = springapplication.run(application.class, args);         logservice.info(application.class.getname(), "service started...");     } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -