請(qǐng)教關(guān)于泛型自動(dòng)裝配的問(wèn)題,@Autowired失敗,s1和s2都是null,報(bào)錯(cuò)說(shuō)循依賴(Circular reference )
老師
你好,我按照你的代碼測(cè)試StoreConfig類,但是無(wú)法Autowired s1和s2,報(bào)錯(cuò)信息如下:
我的Spring版本是4.1.4。請(qǐng)幫忙看一下,謝謝。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springtest.beanannotation.store.Store com.springtest.beanannotation.store.StoreConfig.s1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringStoreTest' defined in com.springtest.beanannotation.store.StoreConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.springtest.beanannotation.store.Store]: Circular reference involving containing bean 'storeConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'stringStoreTest' threw exception; nested exception is java.lang.NullPointerException
各個(gè)類的代碼如下:
//Interface?Store<T> package?com.springtest.beanannotation.store; public?interface?Store<T>?{ } //IntegerStore package?com.springtest.beanannotation.store; public?class?IntegerStore?implements?Store<Integer>?{ } //StringStore? package?com.springtest.beanannotation.store; public?class?StringStore?implements?Store<String>?{ public?void?init(){ System.out.println("This?is?Init."); } public?void?destroy(){ System.out.println("This?is?Destroy."); } } //StoreConfig? @Configuration @ImportResource("classpath:config.xml") public?class?StoreConfig?{ @Autowired private?Store<String>?s1; @Autowired private?Store<Integer>?s2; @Bean public?StringStore?stringStore(){ return?new?StringStore(); } @Bean public?IntegerStore?integerStore(){ return?new?IntegerStore(); } @Bean(name="stringStoreTest") public?Store?stringStoreTest(){ System.out.println("s1:"+s1.getClass().getName()); System.out.println("s2:"+s2.getClass().getName()); return?new?StringStore(); } } //TestStore? public?class?TestStore?extends?UnitTestBase?{ public?TestStore(){ super("classpath*:spring-beanannotation.xml"); } @Test public?void?testG(){ StringStore?store?=?super.getBean("stringStoreTest"); } } //配置文件 <?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.xsd ????????http://www.springframework.org/schema/context ????????http://www.springframework.org/schema/context/spring-context.xsd"?> ???????? ????????<context:component-scan?base-package="com.springtest.beanannotation"></context:component-scan> ???????? ?</beans>
2015-01-12
代碼沒(méi)問(wèn)題,可能是spring版本不同,你試試看用spring4.0.5,所有例子在這個(gè)版本下是都能正常運(yùn)行的。
2016-09-10
//自動(dòng)注入失敗,加上@Qualifier("name")就行啦
@Autowired
@Qualifier("stringStore")
private Store<String> s1;
@Autowired
@Qualifier("integerStore")
private Store<Integer> s2;
2016-03-03
@Autowired
@Qualifier("stringStore")
private Store<String> s1;// 基于泛型的自動(dòng)裝配 stringStore
@Autowired
@Qualifier("integerStore")
private Store<Integer> s2;// 基于泛型的自動(dòng)裝配 integerStore
2015-09-24
新版本加入@Qualifier 做限定就好了