spring+struts2 启动注入bean成功,运行中bean为null

web.xml


contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener

applicationContext.xml

<bean id="tangService" class="com.bander.service.TangService"
          p:dao-ref="tangDao"/>
    <bean id="tangDao" class="com.bander.dao.TangDAO"
          p:sessionFactory-ref="sessionFactory"/>
    <bean id="searchAction" class="com.bander.action.SearchAction" p:service-ref="tangService"/>

已确认在所有引用bean的类中都有setter和getter,在junit中测试是没有问题的,测试代码如下


 @Test
public void testService(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    SearchAction action = context.getBean("searchAction", SearchAction.class);
    PageModel<PoetryBlock> pm = action.getService().searchPoet("李白",1,10);
    List<PoetryBlock> pbs = pm.getDatas();
    for(PoetryBlock pb : pbs){
        System.out.println(pb.getTitle());
    }
}
所以推测是不是我的监听器配得有问题,以至于在web环境下无法使用spring管理bean,大神们帮我看看






2个回答

好吧,第一次发帖有点儿乱

web.xml

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

web.xml是不是没有配置servlet,下面附上一个配置

 <?xml version="1.0" encoding="utf-8" ?>
<web-app
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">

    <!--配置spring mvc 开始-->
    <servlet>
        <servlet-name>let'sGo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/let'sGo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>let'sGo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--SPRING MVC 配置结束-->

    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>
</web-app>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!