请注意,第一个applicationContext作为web.xml;的一部分加载;在下面提到。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>META-INF/spring/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>myOwn-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>META-INF/spring/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如下所示,代码还将尝试创建另一个applicationContext。
private static final ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
见差异之间beans.xml和applicationContext.xml
如果appliationContext.xml在<META-INF/spring/>已经声明了<import resource="beans.xml"/>,然后这个appliationContext.xml被加载beans.xml的同一位置下META-INF/spring的appliationContext.xml。
哪里 在代码中;如果像下面这样声明
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
这是WEB-INF/classes在eclipse中的OR处查找bean.xml src/main/resources。
[如果添加了beans.xmlat,src/main/resources则可能在WEB-INF/classes创建WAR时将其放置在。]
因此,总共查找了两个文件。
我通过在导入时添加类路径查找来解决此问题,applicationContext.xml如下所示
<import resource="classpath*:beans.xml" />
并删除了ClassPathXmlApplicationContext("beans.xml")Java代码中的这一行,因此将仅加载一个ApplicationContext。