当前位置: 代码迷 >> J2EE >> spring报空值异常,求大神帮小弟我指出异常
  详细解决方案

spring报空值异常,求大神帮小弟我指出异常

热度:58   发布时间:2016-04-17 23:35:26.0
spring报空值错误,求大神帮我指出错误啊
我这里在spring里面注入之后还是报空值错误啊,求大神帮忙指出下错误啊!

这是报错

java.lang.NullPointerException
at com.buybal.ums.servlet.OrderBusinessServlet.doOrderService(OrderBusinessServlet.java:114)
at com.buybal.ums.servlet.OrderBusinessServlet.doPost(OrderBusinessServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)


以下是xml文件

applicationContext.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<import resource="classpath:applicationContext-clients.xml" />


<context:component-scan base-package="com.buybal.ums.plugin.service" />
</beans>


applicationContext-clients.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:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">

<bean id="as" class="com.buybal.ums.plugin.client.AsyncOrderMgrNoticeSendClient">
<property name="jmsTemplate">
<bean class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestinationName" value="${jms_queue_phoneFrontService}" />
</bean>
</property>
</bean>

</beans>
相关代码:
package com.buybal.ums.plugin.client;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class AsyncOrderMgrNoticeSendClient {
private JmsTemplate jmsTemplate;

public void publish(final String json) {
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(json);
}
});
}

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

}

package com.buybal.ums.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.buybal.ums.common.DataConstant;
import com.buybal.ums.listener.AsyncOrderMgrNoticeListener;
import com.buybal.ums.plugin.client.AsyncOrderMgrNoticeSendClient;
import com.buybal.ums.plugin.client.OrderMgrClient;
import com.buybal.util.JsonUtil;
@WebServlet("/OrderBusinessServlet")
public class OrderBusinessServlet extends HttpServlet {
AsyncOrderMgrNoticeSendClient as;
OrderMgrClient orderMgrClient;
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
orderMgrClient = (OrderMgrClient) ctx.getBean("orderMgrClient");
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
restStr = doOrderService(JsonUtil.map2json(map));
} catch (Exception e) {
e.printStackTrace();
}
response.getWriter().write(restStr);
}

private String doOrderService(String jSon) throws Exception {
Map<String, String> retMap = new HashMap<String, String>();
String result = "";
try
as.publish(jSon);
} catch (Exception e) {
e.printStackTrace();
retMap.put("RespMsg", "异常");
logger.info("异常");
return JsonUtil.map2json(retMap);
}
return result;

}
public AsyncOrderMgrNoticeSendClient getAs() {
return as;
}
public void setAs(
AsyncOrderMgrNoticeSendClient as) {
this.as = as;
}

}
------解决思路----------------------
public class OrderBusinessServlet extends HttpServlet {
AsyncOrderMgrNoticeSendClient as; // 这里没有初始化
可以试试
public class OrderBusinessServlet extends HttpServlet {
@Autowired
AsyncOrderMgrNoticeSendClient as;
------解决思路----------------------
<context:component-scan base-package="com.buybal.ums.plugin.service" />

servlet 不在这个包里面下面吧 annotation也不会起作用的
------解决思路----------------------
在init方法里面给as赋值。

as = (AsyncOrderMgrNoticeSendClient) ctx.getBean("as");
  相关解决方案