Java 主类中的Spring Boot@Autowired正在变为null
我想连接到Sonic Broker主题并侦听任何传入的XML消息。我做了下面的事情 Application.javaJava 主类中的Spring Boot@Autowired正在变为null,java,spring-boot,sonicmq,Java,Spring Boot,Sonicmq,我想连接到Sonic Broker主题并侦听任何传入的XML消息。我做了下面的事情 Application.java @SpringBootApplication @ComponentScan({"com.mainpack", "com.msgpack.jms"}) @EnableJms public class Application extends SpringBootServletInitializer { @Autowired private JmsTopicListener jmsT
@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);
}
}
}
@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 A Applications Message");
} else {
LogService.info(this.getClass().getName(), "Recieved Message Does not contain Applications Tag");
}
} catch (Exception ex) {
LogService.info(this.getClass().getName(), "Exception: " + ex.getMessage());
}
}
}
}
MsgListener.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);
}
}
}
@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 A Applications Message");
} else {
LogService.info(this.getClass().getName(), "Recieved Message Does not contain Applications Tag");
}
} catch (Exception ex) {
LogService.info(this.getClass().getName(), "Exception: " + ex.getMessage());
}
}
}
}
当我运行这段代码时,我在Application.java
中的jmsTopicListener.listenMessage()
行中得到了null指针
我在这里犯了什么错误?有没有一种方法可以改进这一点(我的意思是用更少的代码完成工作)
注意:com.mainpack有类Application.java
和ApplicationProp.java
com.msgpack.jms有JmsTopicListener.java
和MsgListner.java
来自记录器的错误:
ERROR [2015-07-14 14:34:52] [com.mainpack.Application] [localhost-startStop-1] - [Exception: ]java.lang.NullPointerException
at com.mainpack.Application.onStartup(Application.java:33)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1768)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
onStartup
在应用程序生命周期的早期由servlet容器调用,并在由servlet容器(而不是Spring Boot)创建的类的实例上调用。这就是为什么jmsTopicListener
为null
您可以使用带有@PostConstruct
注释的方法,而不是在启动时重写。一旦Spring创建了应用程序的实例并注入了任何依赖项,它就会被调用:
@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...");
}
}
加上
@PostConstruct
方法,并从该方法运行代码,它将起作用。是否可以打印完整的堆栈跟踪here@kbird:添加堆栈跟踪。请参阅更新后的帖子。当我在tomcat中启动服务时,我在logger中得到了这个。然后什么也没发生。服务没有进一步运行。由于@SpringBootApplication
只是@Configuration
、@EnableAutoConfiguration
和@ComponentScan
的快捷方式,这可能已经是您的问题。您正在尝试访问应用程序类中的@Autowired
资源,该资源既不是@Component
,@Service
。。。afair表示,这将符合Spring的管理资格,并注入依赖项。您应该通过在--debug模式下运行来进行双重检查。@hrrgttnchml:我已经检查了删除@SpringBootApplication
,并添加@Configuration
,@EnableAutoConfiguration
。同样的例外。我不明白为什么spring不能Autowire
那个bean,因为JmsTopicListener
已经被@Component
注释了。@Rajkishan:是的,使用@Component
对JmsTopicListener进行注释很好,但是您还需要让Application.class知道应该注入依赖项<代码>@Configuration
据我所知是不够的。您还应该使用@组件
对其进行注释。但是要注意副作用。当我在tomcat上部署应用程序时,它不使用main()
。因此,我使用onStartUp()
在tomcat上给出“应用程序服务启动消息”
。如果我不使用onStartUp()
,有什么方法可以给出类似“我的服务名称”启动/启动(成功)
或类似的信息吗?谢谢。在我添加了@PostConstruct
之后,它运行良好。应用程序启动时会发布各种事件。有关更多详细信息,请参阅。你可以听其中的一个(或多个)。这样做对启动应用程序的任何一种方式都有效。谢谢。它帮助我添加了一个lotI@PostConstruct,它将我的值打印到控制台,但当我打开本地主机时,我收到一个空值?