我正在尝试使用 @Autowired 将JmsTemplate对象注入到类中,但每当我尝试发送消息时它都会给出 NullPointerException 。在应用程序类中,我可以使用 ApplicationContext
实例化jmsTemplateConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
我无法在任何其他类中执行此操作,因为我没有弄清楚如何获取上下文对象。
这是我的应用程序类:
@EnableAutoConfiguration
public class Application {
private static final String maxFileSize = "512KB";
private static final String maxRequestSize = "512KB";
static String IncomingMailboxDestination = "Responses";
@Bean
Receiver receiver() {
return new Receiver();
}
@Bean
Producer producer() {
return new Producer();
}
@Bean
MessageListenerAdapter adapter(Receiver receiver) {
MessageListenerAdapter messageListener
= new MessageListenerAdapter(receiver);
messageListener.setDefaultListenerMethod("receiveMessage");
return messageListener;
}
@Bean
SimpleMessageListenerContainer container(MessageListenerAdapter messageListener,
ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setMessageListener(messageListener);
container.setConnectionFactory(connectionFactory);
container.setDestinationName(IncomingMailboxDestination);
return container;
}
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize(maxFileSize);
factory.setMaxRequestSize(maxRequestSize);
return factory.createMultipartConfig();
}
public static void main(String[] args) {
// Clean out any ActiveMQ data from a previous run
FileSystemUtils.deleteRecursively(new File("activemq-data"));
// Launch the application
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}
这是Producer类:
@Component("producer")
public class Producer
{
@Autowired
@Qualifier("jmsTemplate")
private JmsTemplate jmsTemplate;
private static final Logger logger = Logger.getLogger(Producer.class);
public void convertAndSendMessage(String destination, Message notification)
{
logger.info("sending message : " + notification.getMessage());
jmsTemplate.convertAndSend(destination, notification);
}
}
这是完整的堆栈跟踪:
java.lang.NullPointerException: null
at ncl.jms.Producer.convertAndSendMessage(Producer.java:35)
at ncl.controllers.ModelController.process(ModelController.java:196)
at ncl.controllers.ModelController.process(ModelController.java:202)
at ncl.controllers.ModelController.runModel(ModelController.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:722)
我在这里缺少什么?
答案 0 :(得分:0)
将jmsTemplate对象从调用类传递给他转换并在我的Producer类中发送消息已经有效了。 但是当我尝试在Producer类中实例化它时,仍然不知道为什么它不起作用。