spring中向一個(gè)單例bean中注入非單例bean的方法詳解
前言
看到這個(gè)題目相信很多小伙伴都是懵懵的,平時(shí)我們的做法大都是下面的操作
@Component public class People{ @Autowired private Man man; }
這里如果Man是單例的,這種寫法是沒有問題的,但如果Man是原型的,這樣是否會(huì)存在問題。
錯(cuò)誤實(shí)例演示
這里有一個(gè)原型(生命周期為prototype)的類
package com.example.myDemo.component; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope(value = "prototype") public class Man { public void eat() { System.out.println("I like beef"); } }
有一個(gè)單例(生命周期為singleton)的類
package com.example.myDemo.component; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Lookup; import org.springframework.stereotype.Component; @Component public class Woman { //使用依賴注入的方式,注入原型的Man @Autowired private Man man; public void eat() { System.out.println("man:"+man); System.out.println("I like fruits"); } }
下面看測(cè)試方法,
package com.example.myDemo; import com.example.myDemo.component.MyFactoryBean; import com.example.myDemo.component.Woman; import com.example.myDemo.po.Student; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.context.ApplicationContext; @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class MyDemoApplication { public static void main(String[] args) { ApplicationContext ac=SpringApplication.run(MyDemoApplication.class, args); Woman woman=(Woman)ac.getBean("woman"); for(int i=0;i<5;i++){ woman.eat(); } } }
看下測(cè)試結(jié)果,
上面的結(jié)果顯示W(wǎng)oman中的man是單例的,因?yàn)?次循環(huán)打印打出的結(jié)果是同一個(gè)對(duì)象,發(fā)生了什么,
Woman是單例的,Man是原型的,我們使用常規(guī)的@Autowired注解注入的卻是同一個(gè)實(shí)例,這里想下為什么Man是一個(gè)對(duì)象,Woman是單例的,意味著在整個(gè)spring容器中只有一個(gè)實(shí)例,在屬性注入的時(shí)候肯定也只會(huì)注入一次,所以其中Man屬性也只能是一個(gè)實(shí)例,出現(xiàn)上圖的結(jié)果也就不稀奇了。
現(xiàn)在有這樣一個(gè)需求要向單例bean中注入原型bean,要怎么實(shí)現(xiàn)這樣的需求
實(shí)現(xiàn)ApplicationContextAware接口
都知道ApplicationContextAware接口是spring提供的一個(gè)擴(kuò)展點(diǎn),實(shí)現(xiàn)該接口的類可以獲得ApplicationContext
Woamn類改成下面的樣子
package com.example.myDemo.component; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Lookup; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class Woman implements ApplicationContextAware { private Man man; private ApplicationContext ac; public void eat() { this.man = (Man) ac.getBean("man"); System.out.println("man:" + man); System.out.println("I like fruits"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.ac = applicationContext; } }
Woman實(shí)現(xiàn)了ApplicationContextAware接口,注入了ApplicaitonContext對(duì)象,然后再eat()方法中通過AppicationContext獲得Man的實(shí)例,看測(cè)試結(jié)果,
可以看到man屬性是多例的也就是符合原型模式的定義。
思考下為什么采用這種方式可以達(dá)到注入原型bean的目的
在eat()方法中使用ApplicationContext的getBean方法獲取Man,eat()方法每執(zhí)行一次均會(huì)調(diào)用一次getBean方法,getbean方法在執(zhí)行的時(shí)候的時(shí)候會(huì)判斷Man的生命周期,如果是原型(prototype)的,那么每調(diào)用一次就會(huì)重新實(shí)例化一個(gè)Man,所以會(huì)出現(xiàn)上述的結(jié)果。
該方法有一個(gè)很大的缺點(diǎn)那就是和spring耦合度太高,不符合降低系統(tǒng)的耦合度的要求。
lookup method
spring也考慮了向一個(gè)單例bean中注入原型bean的情況,提供了@Lookup注解,在XML配置方式下是<lookup-method>標(biāo)簽,這里僅使用注解的方式演示,
Woman類修改如下,
package com.example.myDemo.component; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Lookup; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class Woman { private Man man; public void eat() { this.man = createMan(); System.out.println("man:" + man); System.out.println("I like fruits"); } @Lookup public Man createMan(){ return null; } }
看下測(cè)試結(jié)果,
上圖顯示man是一個(gè)多例的,也就是向單例bean中注入了原型bean,其作用的是@Lookup注解。
通過@Lookup注解便完成了注入原型bean的目的,留個(gè)思考問題spring是如何做到的?
lookup method簽名
被@Lookup注解或<lookup-method>配置的方法有如下要求,
public|protected [abstract] return-type methodName(no-argments)
- 方法可以是public也可以是protected;
- 方法可以是抽象的也可以是非抽象的;
- 方法的返回值是要注入的類型,這里是prototype類型的類;
- 方法沒有入?yún)ⅲ?/li>
- 方法體可以是空的。具體返回值可以是null或任何類型,對(duì)結(jié)果沒有影響;
總結(jié)
分享了向單例bean中注入原型bean的方式,使用lookup的方式會(huì)更簡(jiǎn)潔些。
這還可能是道面試題哦,各位小伙伴注意嘍。lookup的原理下次分享,敬請(qǐng)關(guān)注
到此這篇關(guān)于spring中向一個(gè)單例bean中注入非單例bean的文章就介紹到這了,更多相關(guān)spring注入非單例bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 進(jìn)制轉(zhuǎn)換實(shí)例詳解
這篇文章主要介紹了java 進(jìn)制轉(zhuǎn)換實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04SprintBoot深入淺出講解場(chǎng)景啟動(dòng)器Starter
本篇文章將和大家分享一下 Spring Boot 框架中的 Starters 場(chǎng)景啟動(dòng)器的內(nèi)容,關(guān)于 Starters 具體是用來做什么的,以及在開發(fā) Spring Boot項(xiàng)目前,要如何自定義一個(gè) Starters 場(chǎng)景啟動(dòng)器2022-06-06Java數(shù)據(jù)庫操作庫DButils類的使用方法與實(shí)例詳解
這篇文章主要介紹了JDBC數(shù)據(jù)庫操作庫DButils類的使用方法詳解,需要的朋友可以參考下2020-02-02Java計(jì)時(shí)新姿勢(shì)StopWatch詳解
這篇文章主要介紹了Java計(jì)時(shí)新姿勢(shì)StopWatch,最近公司來了個(gè)大佬,從他那里學(xué)到不少東西,其中一個(gè)就是計(jì)時(shí)的新姿勢(shì)「StopWatch」,需要的朋友可以參考下2019-07-07Mybatis?MappedStatement類核心原理詳解
這篇文章主要介紹了Mybatis?MappedStatement類,mybatis的mapper文件最終會(huì)被解析器,解析成MappedStatement,其中insert|update|delete|select每一個(gè)標(biāo)簽分別對(duì)應(yīng)一個(gè)MappedStatement2022-11-11SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問題解決
這篇文章給大家介紹了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依賴不兼容問題,文中通過代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了桶排序的概念、原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05