spring 注入 空指针怎么办?
我的applicationContextxml<beanid="sessionFactory"class="org.springframework.orm.hiberna...
我的applicationContext xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="DButil" class="util.DButil">
<property name="sf">
<ref local="sessionFactory"/>
</property>
</bean>
servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GB18030");
response.setCharacterEncoding("GB18030");
HttpSession session=request.getSession();
Userbean userbean=(Userbean) session.getAttribute("userbean");
if(userbean==null){
userbean=new Userbean();
}
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
DButil db=(DButil) wac.getBean("DButil");
到最后一行的时候就报空指针!! 展开
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="DButil" class="util.DButil">
<property name="sf">
<ref local="sessionFactory"/>
</property>
</bean>
servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GB18030");
response.setCharacterEncoding("GB18030");
HttpSession session=request.getSession();
Userbean userbean=(Userbean) session.getAttribute("userbean");
if(userbean==null){
userbean=new Userbean();
}
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
DButil db=(DButil) wac.getBean("DButil");
到最后一行的时候就报空指针!! 展开
4个回答
展开全部
你的this.getServletContext() 怎么得到servletContext实例
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
wac没有获取到数据吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
空指针肯定是 wac为null了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询