我想測試的Autowire選項是這樣的:@Autowired註解不工作
@ContextConfiguration(locations = { "classpath:applnContext.xml" })
public class Foo {
@Autowired
private Bar bar;
public Bar getBar() {
return bar;
}
public void setBar(final Bar bar) {
this.bar = bar;
}
public static void main(final String[] args) {
final Foo f = new Foo();
System.out.println(f.getBar());
}
}
和配置文件:
<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="bar" class="entity.Bar"></bean>
<context:annotation-config />
</beans>
但Bar
對象沒有被注入。我在這裏錯過什麼或做錯了什麼?
請注意,我在該類上使用註釋指定了applicationContext文件。