您好,登錄后才能下訂單哦!
這篇文章主要介紹“Spring Bean的優(yōu)先加載方法怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在Spring Bean的優(yōu)先加載方法怎么實(shí)現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring Bean的優(yōu)先加載方法怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
在日常的業(yè)務(wù)開發(fā)中,絕大多數(shù)我們都是不關(guān)注 bean 的加載順序,然而如果在某些場(chǎng)景下,當(dāng)我們希望某個(gè) bean 優(yōu)于其他的 bean 被實(shí)例化時(shí),往往并沒有我們想象中的那么簡(jiǎn)單。
在實(shí)際的 SpringBoot 開發(fā)中,我們知道都會(huì)有一個(gè)啟動(dòng)類,如果希望某個(gè)類被優(yōu)先加載,一個(gè)成本最低的簡(jiǎn)單實(shí)現(xiàn),就是在啟動(dòng)類里添加上依賴
@SpringBootApplication publicclass Application { // 使用構(gòu)造方法的方式來優(yōu)先加載DemoBean public Application(DemoBean demoBean) { demoBean.print(); } public static void main(String[] args) { SpringApplication.run(Application.class); } } 請(qǐng)注意上面的構(gòu)造方法,如果我們希望在應(yīng)用啟動(dòng)之前,demoBean就已經(jīng)被加載了,那就讓 Application 強(qiáng)制依賴它,所以再 Application 的 bean 初始化之前,肯定會(huì)優(yōu)先實(shí)例化demoBean
借助InstantiationAwareBeanPostProcessorAdapter來實(shí)現(xiàn)在 bean 實(shí)例化之前優(yōu)先加載目標(biāo) bean。
publicclass ClientBeanProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { private ConfigurableListableBeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableListableBeanFactory)) { thrownew IllegalArgumentException( "AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory: ">
上面的實(shí)現(xiàn)比較簡(jiǎn)單,借助beanFactory#getBean
來手動(dòng)觸發(fā) bean 的實(shí)例,通過實(shí)現(xiàn)BeanFactoryAware
接口來獲取BeanFactory
,因?yàn)閷?shí)現(xiàn)InstantiationAwareBeanPostProcessor
接口的類會(huì)優(yōu)先于 Bean 被實(shí)例,以此來間接的達(dá)到我們的目的
接下來的問題就是如何讓它生效了,我們這里使用 Import 注解來實(shí)現(xiàn)
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({ClientAutoConfiguration.class, ClientBeanProcessor.class}) public@interface EnableOrderClient { }
package com.spring.master.spring.bean.initbean; import lombok.Getter; import org.springframework.core.env.Environment; import javax.annotation.PostConstruct; /** * @author Huan Lee * @version 1.0 * @date 2020-09-25 11:58 * @describtion 業(yè)精于勤,荒于嬉;行成于思,毀于隨。 */ public class DatasourceLoader { @Getter private String mode; public DatasourceLoader(Environment environment) { this.mode = environment.getProperty("config.save.mode"); System.out.println("init DatasourceLoader for:" + mode); } @PostConstruct public void loadResourcres() { System.out.println("開始初始化資源"); } } package com.spring.master.spring.bean.initbean; import org.springframework.stereotype.Component; /** * @author Huan Lee * @version 1.0 * @date 2020-09-25 12:01 * @describtion 業(yè)精于勤,荒于嬉;行成于思,毀于隨。 */ @Component public class DemoBean { public DemoBean() { System.out.println("demo bean init!"); } public void print() { System.out.println("print demo bean "); } }
package com.spring.master.spring.bean.initbean; import org.springframework.context.annotation.Import; import java.lang.annotation.*; /** * @author Huan Lee * @version 1.0 * @date 2020-09-25 11:59 * @describtion 業(yè)精于勤,荒于嬉;行成于思,毀于隨。 */ @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({ClientAutoConfiguration.class, ClientBeanProcessor.class}) public @interface EnableOrderClient { }
package com.spring.master; import com.spring.master.spring.bean.initbean.DemoBean; import com.spring.master.spring.bean.initbean.EnableOrderClient; import com.spring.master.spring.bean.lifecycle.Person; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @EnableOrderClient @SpringBootApplication public class SpringMasterApplication { public SpringMasterApplication(DemoBean demoBean) { demoBean.print(); } public static void main(String[] args) { SpringApplication.run(SpringMasterApplication.class, args); } } 啟動(dòng)服務(wù)輸出: init DatasourceLoader for:null 開始初始化資源 com.spring.master.spring.bean.initbean.DatasourceLoader@458342d3 demo bean init! print demo bean
到此,關(guān)于“Spring Bean的優(yōu)先加載方法怎么實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。