JavaSpring使用bean声明自动查找和解析xml文件

JavaSpring使用bean声明自动查找和解析xml文件,java,spring,spring-mvc,Java,Spring,Spring Mvc,我最近在玩一个复杂的SpringMVC应用程序。我将servlet调度器设置为自动查找控制器类 <!-- Scans for application @Components to deploy --> <context:component-scan base-package="com.example."/> <context:annotation-config/> <context:spring-configured /> <tx:annot

我最近在玩一个复杂的SpringMVC应用程序。我将servlet调度器设置为自动查找控制器类

<!-- Scans for application @Components to deploy -->
<context:component-scan base-package="com.example."/>
<context:annotation-config/>
<context:spring-configured />
<tx:annotation-driven transaction-manager="transactionManager"/>

据我所知,它会遍历所有jar并尝试查找所有控制器

有趣的是,在类路径上的一个JAR中,我有以下文件myFile.xml,其中包含以下内容:

<?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-3.0.xsd">

<!-- This bean is the parent ApplicationContext for the WebApplicationContexts defined in the WARs. 
     The context files listed here should contain beans that are used by all WARs, for example Services and DAOs. -->
<bean id="grants-app.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>/example/config.xml</value>
            <value>/example/app-config.xml</value>
        </list>
    </constructor-arg>
</bean>
</beans> 

/示例/config.xml
/示例/app-config.xml

不知怎的,Spring获取了这个文件,并试图创建这个文件中定义的bean。我不理解这种行为——我只是告诉Spring寻找控制器类。有人能解释一下这里发生了什么吗?

您需要查看您的web.xml。 当您的每个web应用程序都希望访问常见的源代码(如服务和DAO)(可以像您的情况一样位于EAR中的jar中)时,您可以在上下文参数中指定它,如下面的示例:

    <!--  root application context -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:rootContextBeans.xml</param-value>
    </context-param>
    <!--  shared service layer - parent application context -->
    <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>classpath:myFile.xml</param-value>
    </context-param>
    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>servicelayer-context</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

上下文配置位置
类路径:rootContextBeans.xml
定位器工厂选择器
类路径:myFile.xml
parentContextKey
服务层上下文
org.springframework.web.context.ContextLoaderListener
而myFile.xml将类似于

    <beans>
      <bean id="servicelayer-context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
                <value>/example/config.xml</value>
                <value>/example/app-config.xml</value>
            </list>
        </constructor-arg>
      </bean>
    </beans>

/示例/config.xml
/示例/app-config.xml

是web.xml中的ContextLoaderListener/contextLoader(较旧的spring版本)从myFile.xml加载了这些文件

是从config.xml和app-config.xml文件加载bean吗?是的,spring继续并尝试加载这些bean。我认为这根本不可能。我编辑了这个问题以显示servlet dispatcher的所有设置