前言
成都網(wǎng)站建設(shè)、做網(wǎng)站的開發(fā),更需要了解用戶,從用戶角度來(lái)建設(shè)網(wǎng)站,獲得較好的用戶體驗(yàn)。成都創(chuàng)新互聯(lián)公司多年互聯(lián)網(wǎng)經(jīng)驗(yàn),見的多,溝通容易、能幫助客戶提出的運(yùn)營(yíng)建議。作為成都一家網(wǎng)絡(luò)公司,打造的就是網(wǎng)站建設(shè)產(chǎn)品直銷的概念。選擇成都創(chuàng)新互聯(lián)公司,不只是建站,我們把建站作為產(chǎn)品,不斷的更新、完善,讓每位來(lái)訪用戶感受到浩方產(chǎn)品的價(jià)值服務(wù)。
這段時(shí)間在學(xué)習(xí)Spring,依賴注入DI和面向切面編程AOP是Spring框架最核心的部分。這次主要是總結(jié)依賴注入的bean的裝配方式。
什么是依賴注入呢?也可以稱為控制反轉(zhuǎn),簡(jiǎn)單的來(lái)說,一般完成稍微復(fù)雜的業(yè)務(wù)邏輯,可能需要多個(gè)類,會(huì)出現(xiàn)有些類要引用其他類的實(shí)例,也可以稱為依賴其他類。傳統(tǒng)的方法就是直接引用那個(gè)類對(duì)象作為自己的一個(gè)屬性,但如果我們每次創(chuàng)建這個(gè)類的對(duì)象時(shí),都會(huì)創(chuàng)建依賴的類的對(duì)象,還有如果那個(gè)類將來(lái)可能不用了,還需要到這個(gè)類去刪除這個(gè)對(duì)象,那破壞了代碼的復(fù)用性和導(dǎo)致高度耦合!
依賴注入的出現(xiàn)可以很好地解決這個(gè)問題,依賴注入就是由系統(tǒng)負(fù)責(zé)協(xié)調(diào)類的依賴對(duì)象的創(chuàng)建,我們無(wú)需自己去顯示的創(chuàng)建依賴對(duì)象,而是由系統(tǒng)給我們注入這個(gè)對(duì)象,系統(tǒng)控制了這個(gè)對(duì)象的創(chuàng)建,也稱為控制反轉(zhuǎn)。
Spring給我們注入對(duì)象有三種方式:
第一種:
spring從兩個(gè)角度實(shí)現(xiàn)自動(dòng)化裝配:組件掃描和自動(dòng)裝配。
當(dāng)對(duì)一個(gè)類標(biāo)注@Component注解時(shí),表明該類會(huì)作為組件類,spring將為這個(gè)類創(chuàng)建bean。當(dāng)在應(yīng)用文中引用這個(gè)bean,spring會(huì)自動(dòng)掃描事先指定的包查找這個(gè) bean。但spring默認(rèn)是不啟用組件掃描的,可以在XML中配置加上<context:component-scan base-package="xx"/>
。還有一種方法:在新建一個(gè)配置類,類中可以什么不用寫,在配置類上加上@ComponentScan注解,spring會(huì)自動(dòng)掃描改配置類所在的包,一般應(yīng)該傾向xml配置。下面是一個(gè)bbs論壇系統(tǒng)用戶發(fā)帖的功能小例子:
package bbs.dao; @Component public interface Postdao { /* *用戶發(fā)帖 ,post表添加帖子信息 */ public int addpost(@Param("title") String title,@Param("content") String content,@Param("userid") int userid); } package bbs.dao; @Component public interface Userdao { /* * 用戶發(fā)帖后,user表將用戶發(fā)帖數(shù)加一 */ public int addpost(int userid); }
再在bbs.service包中創(chuàng)建一個(gè)postservice接口及其實(shí)現(xiàn)類,依賴Postdao和Userdao。
package bbs.service; public interface PostService { /* 用戶發(fā)帖后,先添加帖子信息再更新用戶發(fā)帖數(shù)量 */ public void addpost(String title,String content,int userid); } package bbs.service; @Component public class PostserviceImpl implements PostService { private Postdao postdao; private Userdao userdao; // @Autowired // public void setPostdao(Postdao postdao) // { // this.postdao=postdao; // } // // @Autowired // public void setUserdao(Userdao userdao) // { // this.userdao=userdao; // } @Autowired public PostserviceImpl(Postdao postdao,Userdao userdao) { this.userdao=userdao; this.postdao=postdao; } public void addpost(String title, String content, int userid) { int i=postdao.addpost(title, content, userid); int j=userdao.addpost(userid); if(i==1&j==1) System.out.println("發(fā)帖成功"); else System.out.println("發(fā)帖失敗"); } }
@Component在接口實(shí)現(xiàn)上注解就可以,但發(fā)現(xiàn)在userdao、postdao接口也加上了,其實(shí)可以去掉,因?yàn)槲也捎胢ybatis在xml中配置數(shù)據(jù)庫(kù)的操作,動(dòng)態(tài)實(shí)現(xiàn)dao接口。等下會(huì)提到。上面代碼出現(xiàn)的@Autowired注解實(shí)現(xiàn)bean自動(dòng)裝配,會(huì)在spring應(yīng)用上下文中的組件類尋找需求的bean。一般有兩種裝配方式:構(gòu)造器和Setter方法(其他方法名也行,只要能夠使注入的bean成為這個(gè)類的屬性就行)
也可能出現(xiàn)spring沒有查找到匹配的bean會(huì)拋出異常,在@Autowired加上required=false
,如果沒有匹配的bean時(shí),spring會(huì)使這個(gè)bean處于未裝配的狀態(tài),沒有裝配成功。還有可能會(huì)出現(xiàn)相同名字的bean有很多個(gè),會(huì)產(chǎn)生歧義,一般在組件類上添加注解@Qualifier()
括號(hào)寫這個(gè)bean的id,在注入時(shí)也加上@Qualifier()
,寫上bean的id。像下面:
@Component @Qualifier("postdao") public interface Postdao{ . . . . } @Component @Qualifier("userdao") public interface Userdao{ . . . . } @Autowired @Qualifier("usedao") public void setUserdao(Userdao userdao) {. . . } @Autowired @Qualifier("postdao") public void setUserdao(Postdao postdao) {. . . }
由于java不允許在同一個(gè)條目上重復(fù)出現(xiàn)相同類型的多個(gè)注解,所有注入采用set方式。但是其實(shí)可以創(chuàng)建自定義的限定符注解。這里就不介紹啦。
第二種:
通過java代碼裝配bean
一般通過組件掃描和自動(dòng)裝配方式就比較方便了,但如果由于需求我們要使用第三方的庫(kù)的類,在這種情況沒有辦法到第三方庫(kù)中去給類加注解,就不能使用第一種方法了。這時(shí)得采用顯示裝配,可以采用java代碼或xml顯示裝配bean。使用java代碼,先新建一個(gè)配置類JavaConfig,里面都是配置所需的bean,不應(yīng)該有業(yè)務(wù)邏輯代碼,所以單獨(dú)建一個(gè)類。
@Configuration @ContextConfiguration(locations = {"classpath:spring/spring-dao.xml","classpath:scan.xml"}) public class bbsConfig{ private Postdao postdao; private Userdao userdao; @Bean(name="postservice") public PostService getPost() { return new PostserviceImpl(postdao,userdao); }
在對(duì)PostService的bean注入時(shí),同時(shí)又依賴了兩個(gè)bean,postdao和userdao。直接引用beanID就可以,spring會(huì)自動(dòng)地從容器中獲取這些bean,只要他們的配置是正確的就行。這個(gè)例子中userdao、postdao是Mybatis配置自動(dòng)掃描將dao接口生成代理注入到spring的,其實(shí)也算是xml裝配bean??蓞⒖歼@篇文章,寫的挺清楚的。 https://www.jb51.net/article/157618.htm
這里如果再聲明一個(gè)bean,返回的仍是postserviceImpl對(duì)象,和之前的那個(gè)bean完全一樣,是同一個(gè)實(shí)例。一般spring@bean如果是同一個(gè)beanID,默認(rèn)返回的是一個(gè)單例bean,注入的是同一個(gè)實(shí)例。如果修改其中一個(gè)會(huì)都改變的。
不過在這里要注意進(jìn)行測(cè)試時(shí),由于spring的單元測(cè)試和springIoc容器是完全獨(dú)立的,postdao和userdao注入檢測(cè)時(shí)是使用locations加載xml文件,而postservice使用classes加載config類的,但是兩個(gè)不能同時(shí)混用在@ContextConfiguration中。所以非要都測(cè)試的話,就分開測(cè)試吧。
第三種:
在XML中裝配bean
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context"> <import resource="spring/spring-dao.xml"/> <bean id="postservice" class="com.bbs.service.impl.PostserviceImpl"> <constructor-arg ref="postdao"/> <constructor-arg ref="userdao"/> </bean> </beans>
配置postservice的bean時(shí)需要引入兩個(gè)bean,postdao和userdao,放到constructor-arg的標(biāo)簽中,ref指的是依賴的bean的ID。如果是在javaConfig中配置的,就寫@Bean的內(nèi)容。如果是@Component就寫@Qualifier的內(nèi)容。這里是引入的是動(dòng)態(tài)實(shí)現(xiàn)的dao接口的bean,是在spring-dao.xml中配置的,引入這個(gè)配置文件就可以自動(dòng)獲得beanID。
混合使用三種裝配:
1.在類上可以使用@ import(bbsConfig.class)
組合其他java注解
2.在類上使用@ imortResource("classpath:spring-dao.xml")
組合其他xml注解
3.在類上可以使用@ContenxtConfiguration
包含class或者xml
4.在xml中可以用<import resource="spring-dao.xml">
引入xml注解,也可以使用<bean class="com.bbs.dao.Userdao">
引入java注解
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前題目:spring裝配bean的3種方式總結(jié)
網(wǎng)站路徑:http://www.rwnh.cn/article42/pcogec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、小程序開發(fā)、服務(wù)器托管、用戶體驗(yàn)、標(biāo)簽優(yōu)化、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)