spring 配置文件被加载两次

如下web.xml示例:

1.用spring的配置加载contextConfigLocation

2.配置spring-mvc的contextConfigLocation

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

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

看了配置,spring下面的文件在springmvc中加载了一次,在context中又加载了一次。

从启动日志和启动时长可以看到。 补充:重复加载还会导致配置的task,schadule任务重复执行!

所以,合理的做法是,把spring-mvc的定义单独摘出来定义在web容器启动的时候加载,名称也换成servlet-mvc.xml避免被spring重复加载, 而在spring的core xml定义中,不要出现mvc的东东

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--查找controller位置的xml文件配置 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 -->
        <param-value>/WEB-INF/classes/servlet-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>  

springmvc的xml单独配置出来,spring本身管spring的,springmvc管springmvc的,这样就ok了

总结:该分开配置的不要合在一起。注意代码使用

时间: 2024-08-07 21:50:08

spring 配置文件被加载两次的相关文章

监听器如何获取Spring配置文件(加载生成Spring容器)

Spring容器是生成Bean的工厂,我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出来放到application里,即在监听器里调用后台商品业务逻辑的方法,也就是说我们需要在监听器里获取Spring中配置的相应的bean.先把监听器创建出来: 1. 创建InitDataListener 创建一个监听器InitDataListener继承Serv

Spring配置文件的加载,及装载多个beans.xml文件

applicationContext.xml 是spring的全局配置文件,用来控制srping的特性 1  手动加载自定义的beans.xml文件 @Test public void testAutoWire() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("ioc/autoWire/beanAutoWire.xml");    //加载包ioc.autoWire下面的b

【Spring】从源码分析Spring配置文件的加载

使用Spring必须在web.xml中写如下配置: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-valu

IDEA导入maven工程以及web.xml中spring配置文件文件加载不到的问题

使用idea导入maven工程,工程只留了src和pom.xml文件 1.从打开idea中导入:File ----> New -----> Project from Existing Sources.如下图: 2.选择你所要导入的项目.点击ok 3.一定要选择; 第二个  :Import project from external model    从外部模型导入项目,然后点击Next 4.下一步......选择你需要的jdk.然后:项目名称一定和文件名称一致,然后点击Filish就OK了 首

Spring task配置,及解决加载两次的方法

? 关于 启动Task任务同时加载两次的解决方法:? 将spring MVC部分的定义另外建立一个文件,同时把Task配置放在此处,然后在web.xml文件中的处加载 <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-pa

spring-boot学习五:Spring boot配置文件的加载位置

1.配置文件的加载位置 spring boot启动的时候会加载全局配置文件application.properties(或者application.yaml)文件,这个文件默认的位置在: 即classpath:/的相对路径下.这个配置文件除了可以在resource下之外,还可以在如下位置: 在classpath:/config/即resources/config下: 在当前工程文件的config文件夹下: 在当前工程的相对路径下: 2.配置文件的加载顺序 以上所说的四个文件的优先级如下: app

Spring Boot配置文件的加载顺序

配置文件的加载顺序, 后加载的会覆盖先加载的:也就是properties配置文件的内容会替换掉.yml及.yaml文件的内容 原文地址:https://www.cnblogs.com/niwotaxuexiba/p/10849376.html

web.xml中如何设置配置文件的加载路径

web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志.数据库.spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件可以放到 SRC目录下或者可以放到WEB-INF根目录下 第一种在web.xml中这样配置: <context-param> <param-name >contextConfigLocation </param-name > <param-value >clas

spring 如何动态加载properties文件的内容

1. 在xml中配置你的properties路径: <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <!-- 指定资源文件基名称 jdbc为文件名,不包含扩展名 --&g