我有一个Jersey Spring应用程序。我无法弄清楚是什么导致它引入我不告诉它使用的Spring XML配置文件。
这是我的applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!--<import resource="classpath:com/lala/spring/settings.xml"/>-->
<context:component-scan base-package="com.lala.service,
com.lala.bin"/>
</beans>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
settings.xml位于/com/lala/spring/settings.xml那么为什么Tomcat会打印这个:
Feb 2, 2012 11:35:57 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Feb 2, 2012 11:35:57 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Thu Feb 02 11:35:57 EST 2012]; root of context hierarchy
Feb 2, 2012 11:35:57 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
Feb 2, 2012 11:35:57 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/syncwords/spring/settings.xml]
Feb 2, 2012 11:35:58 AM org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
如果我取消评论实际的&lt; import&gt;然后我看到“正在加载XML ... settings.xml”TWICE并得到警告,它的bean被双重初始化。如果我删除“组件扫描”并手动定义一些bean,那么一切正常。
我正在使用Spring 3.1.0-RELEASE
答案 0 :(得分:0)
这让调试变得令人抓狂,因为Netbeans / Tomcat在运行之间没有正确清理它的大脑。 (事实证明你必须手动删除Tomcat目录并执行Clean&amp; Build。)但是罪魁祸首是使用@RunAs(SpringJUnit4ClassRunner.class)和@ContextConfiguration(我的源代码中有测试类)注释的测试类。
我认为这是Spring中的一个错误,并且会将其作为文件提交,因为测试类会创建自己的上下文(通过特殊的类运行器),并且组件扫描不会拾取它。