Why is Spring component-scan finding XML files?
Question
I have a Jersey Spring application. I can't figure out what is causing it to pull in a Spring XML config file that I'm not telling it to use.
Here is my 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 is located in /com/lala/spring/settings.xml So why the hell is Tomcat printing this:
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
If I un-comment the actual <import> then I see "Loading XML...settings.xml" TWICE and get warnings that its beans are doubly initialized. If I delete "component-scan" and manually define some beans, then everything is OK.
I'm using Spring 3.1.0-RELEASE
Solution
This was maddening to debug, because Netbeans/Tomcat doesn't clear its brains properly between runs. (Turns out you have to delete the Tomcat directory manually and do Clean&Build.) But the culprit was a test class annotataed with @RunAs(SpringJUnit4ClassRunner.class) and @ContextConfiguration (I have test classes among my sources).
I consider this a bug in Spring and will file it as such, because test classes create their own contexts (via the special class runner) and have no place being picked up by component-scan.