SpringMVC的监听器无法加载Bean原因

大部分Web系统都希望在启动时可以加载一系列的资源,这样在应用中使用这些资源的时候就不再需要重新加载。

通常我们的做法就是在web.xml 中配置对应的listener,这个listener是继承自javax.servlet.ServletContextListener,然后在覆写的contextInitialized方法中可以进行自由的初始化。

-------------------------------------------------我是分割线-------------------------------------------------

在contextInitialized中肯定是需要获取Spring给我们配置的Bean的,如何获取很简单:

WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
TestService testService = springContext.getBean(TestService.class);

但是我们可能会遇到无法获取到Bean的情况,什么原因?很简单,因为Spring还没有帮我们实例化完成

这个时候需要我们检查下web.xml的配置,通常情况下采用如下的配置顺序:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Blockchain Web Application</display-name>
  <!-- Spring和mybatis的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 编码过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- Spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 防止Spring内存溢出监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.jd.ofc.system.SystemListener</listener-class>
  </listener>

  <!-- Spring MVC servlet -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

重点关注其中applicationContext.xml的顺序和spring-mvc.xml的顺序

applicationContext.xml放在 context-param中的,它的加载顺序高于listener,所以这个相关的Bean会在加载到listener时已经加载完成,但是spring-mvc.xml的加载顺序是位于servlet中,要晚于listener,因此service的bean和dao的bean放在applicationContext.xml中,controller的bean可以放在spring-mvc.xml中。

另外需要注意一点就是不再需要在applicationContext.xml显式的引用spring-mvc.xml,但是需要引dao相关的xml(如果有的话)

转载于:https://www.cnblogs.com/bruceshao/articles/8534498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值