请注意,第一个 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.xmlapplicationContext.xml
如果appliationContext.xmlunder<META-INF/spring/>已声明 with<import resource="beans.xml"/>那么这appliationContext.xml将加载under的beans.xml相同位置META-INF/spring。appliationContext.xml
然而; 在代码中;如果它声明如下
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
WEB-INF/classes这是在 eclipse 中的 OR 中查看 beans.xml src/main/resources。
[如果您添加了beans.xmlatsrc/main/resources那么它可能会在WEB-INF/classes创建 WAR 时放置在。]
因此,总共查找了两个文件。
我已经通过在导入时添加类路径查找解决了这个问题,applicationContext.xml如下所示
<import resource="classpath*:beans.xml" />
并删除了ClassPathXmlApplicationContext("beans.xml")java代码中的这一行,这样只会加载一个ApplicationContext。