<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

解决方案 »

  1.   

    程序有set get吗???把空指针异常贴出来看看
      

  2.   

    有set,没get,好像没那个必要吧?
    我别的注入都成功了,区别就是成功的是把它注入到action里面了,这个注入到listener里就不行 了,我打印了要注入的对象,是null的
      

  3.   

    先风情解析过程先解析web.xml的Listener ,当你还加载spring的时候他就可能先解析了如果你用plugin的方式的话,linstener先执行 而还没得到spring的注入比如经常把定时器放到web的Listener中,会有null的错误放到spring注入之后试一试
      

  4.   

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <!-- Moudle 层配置 -->
    <bean id="customerDao" class="last.soul.dao.impl.CustomerDaoImpl">
    </bean>
    <bean id="orderDao" class="last.soul.dao.impl.OrderDaoImpl"></bean><!-- Service 层配置 -->
    <bean id="customerService" class="last.soul.service.impl.CustomerServiceImpl">
    <property name="customerDao">
    <ref local="customerDao"></ref>
    </property>
    </bean>
    <bean id="orderService" class="last.soul.service.impl.OrderServiceImpl"> 
    <property name="orderDao">
    <ref local="orderDao"></ref>
    </property>
    </bean>
    <!-- Web 层配置 -->
    <bean id="RegisterAction" class="last.soul.web.RegisterAction" scope="prototype">
    <property name="customerService">
    <ref local="customerService"/>
    </property>
    </bean>
    <bean id="LoginAction" class="last.soul.web.LoginAction" scope="prototype">
    <property name="customerService">
    <ref local="customerService"></ref>
    </property>
    </bean></beans>上这这个是我的applicationContext.xml.package last.soul.web;
    import java.util.Map;import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;import last.soul.bean.Book;
    import last.soul.common.BeanFactory;
    import last.soul.common.exception.OrderServiceException;
    import last.soul.service.IOrderService;public class ContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    ServletContext stx = arg0.getServletContext();
    stx.removeAttribute("books");
    } public void contextInitialized(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
    ServletContext stx = arg0.getServletContext();
    IOrderService orderService = 
    (IOrderService)BeanFactory.getBean("orderService");
    try {
    Map<Long,Book> books =orderService.listAllBook();//调用orderService的方法
    System.out.println(books+"in listenter");
    stx.setAttribute("books", "booksdd");
    } catch (OrderServiceException e) {
    e.printStackTrace();
    }
    }}这是我的Listener代码。我的web层的两个action调用customerService,customerService调用customerDao都没问题
    当我在Listener里面调用orderService时候也是成功的(没有用spring注入),orderService里面我注入了orderDao
    代码如上所示,在orderServiceImpl中打印orderDao对象为null。
    问题在哪里,各位指点一下,莫非Listener有什么说法,和applicationContext.xml加载的先后有关吗?applicationContext.xml它是什么时候加载的?
      

  5.   

    我确实是用plugin方式注入的,我就是想在整个程序运行之前做一些工作(也就是把一些东西从数据库中查出来,放到application中去),不能用listener的话,用filter应该可以吧,intercepter也应该可以的吧
      

  6.   

    可以试试下面的方式WebApplicationContext springCtx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    Srervice service = (Service)srpingCtx.getBean("Service");