the Spring component-scan seems not to be working. I am using Spring 3.1 and tomcat 7.0.
This is how my applicationContext.xml looks like:
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.abc"/>
<bean id="myBean" class="com.efg.test.MyBean" />
</beans>
I have a class like this:
package com.abc.test
@Component
class Test {
@Autowired
private static MyBean myBean;
public static MyBean getMyBean() { return myBean; }
public static void setMyBean(MyBean bean) { myBean = bean; }
}
I had expected that Test will be initialized by Spring on startup of the webapplication and that myBean will be injected automatically, so if I call any (static) method of Test I have a not null reference to myBean.
However, myBean does not get injected and is always null (myBean itself gets initialized as singleton, it just is not injected). What else do I have to do to get this working? If I add the Test-class to the applicationContext.xml everything is working. My guess is that Test is just not initialized by Spring and therefore not wired.
Update: I need to be able to access the autowired field from a static context (a method of this class is called as a JSP function), therefore it has to be static.