第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

請(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>


正在回答

4 回答

代碼沒(méi)問(wèn)題,可能是spring版本不同,你試試看用spring4.0.5,所有例子在這個(gè)版本下是都能正常運(yùn)行的。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

傾真魚(yú) 提問(wèn)者

換了spring4.0.5試了下,不報(bào)錯(cuò),正常輸出了。 只是不明白為什么我用最新版本的Spring卻不支持這種@Autowired。
2015-01-13 回復(fù) 有任何疑惑可以回復(fù)我~

//自動(dòng)注入失敗,加上@Qualifier("name")就行啦

@Autowired
@Qualifier("stringStore")
private Store<String> s1;

@Autowired
@Qualifier("integerStore")
private Store<Integer> s2;

4 回復(fù) 有任何疑惑可以回復(fù)我~
#1

zyxailgq

阿里嘎多
2016-12-10 回復(fù) 有任何疑惑可以回復(fù)我~
#2

zyxailgq

查了一上午的資料都沒(méi)解決 !感動(dòng)
2016-12-10 回復(fù) 有任何疑惑可以回復(fù)我~
#3

未卜先知

4.3.4 成功,比百度還靠譜。。。
2017-01-08 回復(fù) 有任何疑惑可以回復(fù)我~

@Autowired

@Qualifier("stringStore")

private Store<String> s1;// 基于泛型的自動(dòng)裝配 stringStore


@Autowired

@Qualifier("integerStore")

private Store<Integer> s2;// 基于泛型的自動(dòng)裝配 integerStore


2 回復(fù) 有任何疑惑可以回復(fù)我~
#1

凱哥Java

@Autowired @Qualifier("integerStore") private Store<Integer> s2;// 基于泛型的自動(dòng)裝配 integerStore 你使用這個(gè)在bean和調(diào)用是用s2還是integerStore? 你的代碼還有嗎?可以貼出來(lái)看下嗎? 謝謝
2016-05-06 回復(fù) 有任何疑惑可以回復(fù)我~

新版本加入@Qualifier 做限定就好了

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

qq_DragonHeart_0

在這兩個(gè)聲明前頭加入對(duì)應(yīng)的@Qualifier 后運(yùn)行成功,我的spring版本是4.2.5
2016-03-03 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Spring入門篇
  • 參與學(xué)習(xí)       268801    人
  • 解答問(wèn)題       1026    個(gè)

為您帶來(lái)IOC和AOP的基本概念及用法,為后續(xù)高級(jí)課程學(xué)習(xí)打下基礎(chǔ)

進(jìn)入課程

請(qǐng)教關(guān)于泛型自動(dòng)裝配的問(wèn)題,@Autowired失敗,s1和s2都是null,報(bào)錯(cuò)說(shuō)循依賴(Circular reference )

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)