日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Spring注入Bean的幾種方式,還有誰(shuí)不會(huì)?

通過(guò)注解注入Bean

背景

我們的項(xiàng)目一般很大的話,就需要成千上百個(gè)Bean去使用,這樣寫(xiě)起來(lái)就很繁瑣。那么Spring就幫我們實(shí)現(xiàn)了一種通過(guò)注解來(lái)實(shí)現(xiàn)注入的方法。只需要在你需要注入的類(lèi)前面加上相應(yīng)的注解,Spring就會(huì)幫助我們掃描到他們?nèi)?shí)現(xiàn)注入。

10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有疊彩免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

xml掃描包的方式

通過(guò)注解注入的一般形式

一般情況下,注入Bean有一個(gè)最直白,最易懂的方式去實(shí)現(xiàn)注入,下面廢話先不多說(shuō),先貼代碼。

另外,Spring 系列面試題和答案全部整理好了,微信搜索Java面試庫(kù)小程序,可以在線刷題。

Bean類(lèi)

public class MyBean{
}

Configuration類(lèi)

//創(chuàng)建一個(gè)class配置文件
@Configuration
public class MyConfiguration{
//將一個(gè)Bean交由Spring進(jìn)行管理
@Bean
public MyBean myBean(){
return new MyBean();
}
}

Test類(lèi)

與xml有一點(diǎn)不同,這里在Test中,實(shí)例化的不再是ClassPathXmlApplicationContext,而是獲取的AnnotationConfigApplicationContext實(shí)例。

ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
MyBean myBean = cotext.getBean("myBean",MyBean.class);
System.out.println("myBean = " + myBean);

上面的代碼中MyBean也就是我們需要Spring去管理的一個(gè)Bean,他只是一個(gè)簡(jiǎn)單的類(lèi)。而MyConfiguration中,我們首先用@Configuration注解去標(biāo)記了該類(lèi),這樣標(biāo)明該類(lèi)是一個(gè)Spring的一個(gè)配置類(lèi),在加載配置的時(shí)候會(huì)去加載他。

在MyConfiguration中我們可以看到有一個(gè)方法返回的是一個(gè)MyBean的實(shí)例,并且該方法上標(biāo)注著@Bean的注解,標(biāo)明這是一個(gè)注入Bean的方法,會(huì)將下面的返回的Bean注入IOC。

通過(guò)構(gòu)造方法注入Bean

我們?cè)谏梢粋€(gè)Bean實(shí)例的時(shí)候,可以使用Bean的構(gòu)造方法將Bean實(shí)現(xiàn)注入。直接看代碼

Bean類(lèi)

@Component
public class MyBeanConstructor {

private AnotherBean anotherBeanConstructor;

@Autowired
public MyBeanConstructor(AnotherBean anotherBeanConstructor){
this.anotherBeanConstructor = anotherBeanConstructor;
}

@Override
public String toString() {
return "MyBean{" +
"anotherBeanConstructor=" + anotherBeanConstructor +
'}';
}
}

AnotherBean類(lèi)

@Component(value="Bean的id,默認(rèn)為類(lèi)名小駝峰")
public class AnotherBean {
}

Configuration類(lèi)

@Configuration
@ComponentScan("com.company.annotationbean")
public class MyConfiguration{
}

這里我們可以發(fā)現(xiàn),和一般方式注入的代碼不一樣了,我們來(lái)看看新的注解都是什么意思:

@AutoWired

簡(jiǎn)單粗暴,直接翻譯過(guò)來(lái)的意思就是自動(dòng)裝配:wrench:,還不理解為什么叫自動(dòng)裝配:wrench:?看了下一個(gè)注解的解釋你就知道了。若是在這里注入的時(shí)候指定一個(gè)Bean的id就要使用@Qualifier注解。

@Component(默認(rèn)單例模式)

什么??這翻譯過(guò)來(lái)是零件,怎么感覺(jué)像是修汽車(chē)??是的,Spring管理Bean的方法就是修汽車(chē)的方式。我們?cè)谛枰獙⒁粋€(gè)類(lèi)變成一個(gè)Bean被Spring可以注入的時(shí)候加上注解零件@Conmonent,那么我們就可以在加載Bean的時(shí)候把他像零件一樣裝配:wrench:到這個(gè)IOC汽車(chē)上了

在這里我們還有幾個(gè)其他的注解也可以實(shí)現(xiàn)這個(gè)功能,也就是細(xì)化的@Component:

  • @Controller 標(biāo)注在Controller層
  • @Service 標(biāo)注在Service層
  • @Repository 標(biāo)注在dao層

@ComponentScan("")

還是翻譯,零件掃描,我們?nèi)タ纯蠢ㄌ?hào)里的“零件倉(cāng)庫(kù)”里面,哪些“零件”(類(lèi))需要被裝載,Spring就會(huì)去掃描這個(gè)包,將里面所有標(biāo)注了@Component的類(lèi)進(jìn)行注入。

這里的通過(guò)構(gòu)造方法進(jìn)行注入就很好理解了,我們?cè)谘b配MyBean這個(gè)零件的時(shí)候,突然發(fā)現(xiàn)他必須在AnotherBean的基礎(chǔ)上才能安裝到IOC里面,那么我們就在每次裝配MyBean的時(shí)候自動(dòng)裝配:wrench:一個(gè)AnotherBean進(jìn)去。舉個(gè):chestnut:吧:

還是以汽車(chē)為例,我們?cè)诓扔烷T(mén)出發(fā)之前,是不是必須發(fā)車(chē)??這里的AutoWired的內(nèi)容就像發(fā)車(chē),你不發(fā)車(chē),這個(gè)油門(mén)你踩斷都沒(méi)有用,他都不會(huì)走。

通過(guò)set方法注入Bean

我們可以在一個(gè)屬性的set方法中去將Bean實(shí)現(xiàn)注入,看代碼吧

MyBean類(lèi)

@Component
public class MyBeanSet {

private AnotherBean anotherBeanSet;

@Autowired
public void setAnotherBeanSet(AnotherBean anotherBeanSet) {
this.anotherBeanSet = anotherBeanSet;
}

@Override
public String toString() {
return "MyBeanSet{" +
"anotherBeanSet=" + anotherBeanSet +
'}';
}
}

Configuration類(lèi) 和 Test類(lèi)

同上一個(gè),就不貼了

這里我們發(fā)現(xiàn)在setter方法上我們有一個(gè)@AutoWired,與上面不同的是,我們不會(huì)在實(shí)例化該類(lèi)時(shí)就自動(dòng)裝配:wrench:這個(gè)對(duì)象,而是在顯式調(diào)用setter的時(shí)候去裝配。

通過(guò)屬性去注入Bean

我們前面兩種注入的方式諸如時(shí)間不同,并且代碼較多,若是通過(guò)屬性,即就是

@Component
public class MyBeanProperty {

@Autowired
private AnotherBean anotherBeanProperty;

@Override
public String toString() {
return "MyBeanProperty{" +
"anotherBeanProperty=" + anotherBeanProperty +
'}';
}
}

這里我們可以看到我們這個(gè)類(lèi)中需要使用AnotherBean這個(gè)實(shí)例對(duì)象,我們可以通過(guò)@AutoWired去自動(dòng)裝配它。

通過(guò)List注入Bean

MyBeanList類(lèi)

@Component
public class MyBeanList {

private List stringList;

@Autowired
public void setStringList(List stringList) {
this.stringList = stringList;
}

public List getStringList() {
return stringList;
}
}

MyConfiguration類(lèi)

@Configuration
@ComponentScan("annoBean.annotationbean")
public class MyConfiguration {

@Bean
public List stringList(){
List stringList = new ArrayList();
stringList.add("List-1");
stringList.add("List-2");
return stringList;
}
}

這里我們將MyBeanList進(jìn)行了注入,對(duì)List中的元素會(huì)逐一注入。

MyConfiguration類(lèi)

@Bean
//通過(guò)該注解設(shè)定Bean注入的優(yōu)先級(jí),不一定連續(xù)數(shù)字
@Order(34)
public String string1(){
return "String-1";
}

@Bean
@Order(14)
public String string2(){
return "String-2";
}

注入與List中泛型一樣的類(lèi)型,會(huì)自動(dòng)去匹配類(lèi)型,及時(shí)這里沒(méi)有任何List的感覺(jué),只是String的類(lèi)型,但他會(huì)去通過(guò)List的Bean的方式去注入。

通過(guò)Map去注入Bean

@Component
public class MyBeanMap {

private Map integerMap;

public Map getIntegerMap() {
return integerMap;
}

@Autowired
public void setIntegerMap(Map integerMap) {
this.integerMap = integerMap;
}
}
@Bean
public Map integerMap(){
Map integerMap = new HashMap();
integerMap.put("map-1",1);
integerMap.put("map-2",2);
return integerMap;
}

@Bean
public Integer integer1(){
return 1;
}

@Bean
public Integer integer2(){
return 2;
}

同樣這里也具有兩種方式去注入Map類(lèi)型Bean,且第二種的優(yōu)先值高于第一種

以上就是Bean通過(guò)注解注入的幾種方式,大家可以對(duì)比著xml注入的方式去看。


當(dāng)前題目:Spring注入Bean的幾種方式,還有誰(shuí)不會(huì)?
轉(zhuǎn)載來(lái)源:http://www.dlmjj.cn/article/cccohsi.html