<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          將 Bean 放入 Spring 容器中的五種方式

          共 14208字,需瀏覽 29分鐘

           ·

          2023-10-16 10:32

          程序員的成長(zhǎng)之路
          互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享 
          關(guān)注


          閱讀本文大概需要 5 分鐘。

          來(lái)自:blog.csdn.net/weixin_43741092/article/details/120176466

          我們知道平時(shí)在開發(fā)中使用Spring的時(shí)候,都是將對(duì)象交由Spring去管理,那么將一個(gè)對(duì)象加入到Spring容器中,有哪些方式呢,下面我就來(lái)總結(jié)一下

          1、@Configuration + @Bean

          這種方式其實(shí)最常用的一種方式,@Configuration用來(lái)聲明一個(gè)配置類,然后使用 @Bean 注解,用于聲明一個(gè)bean,將其加入到Spring容器中。
          具體代碼如下:

          @Configurationpublic class MyConfiguration {    @Bean    public Person person() {        Person person = new Person();        person.setName("spring");        return person;    }}

          2、@Componet + @ComponentScan

          這種方式也是我們用的比較多的方式,@Componet中文譯為組件,放在類名上面,然后@ComponentScan放置在我們的配置類上,然后可以指定一個(gè)路徑,進(jìn)行掃描帶有@Componet注解的bean,然后加至容器中。
          具體代碼如下:

          @Componentpublic class Person {    private String name;     public String getName() {         return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                '}';    }} @ComponentScan(basePackages = "com.springboot.initbean.*")public class Demo1 {    public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }}

          結(jié)果輸出:

          Person{name='null'}

          表示成功將Person放置在了IOC容器中。

          3、@Import注解導(dǎo)入

          前兩種方式,大家用的可能比較多,也是平時(shí)開發(fā)中必須要知道的,@Import注解用的可能不是特別多了,但是也是非常重要的,在進(jìn)行Spring擴(kuò)展時(shí)經(jīng)常會(huì)用到,它經(jīng)常搭配自定義注解進(jìn)行使用,然后往容器中導(dǎo)入一個(gè)配置文件。
          關(guān)于@Import注解,我會(huì)多介紹一點(diǎn),它有四種使用方式。這是@Import注解的源碼,表示只能放置在類上。

          @Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Import {     /**   * 用于導(dǎo)入一個(gè)class文件     * {@link Configuration @Configuration}, {@link ImportSelector},     * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.     */    Class<?>[] value(); }

          3.1 @Import直接導(dǎo)入類

          代碼示例如下:

          public class Person {    private String name;     public String getName() {         return name;    }     public void setName(String name) {        this.name = name;    }     @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                '}';    }}/*** 直接使用@Import導(dǎo)入person類,然后嘗試從applicationContext中取,成功拿到**/@Import(Person.class)public class Demo1 {     public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }}

          上述代碼直接使用@Import導(dǎo)入了一個(gè)類,然后自動(dòng)的就被放置在IOC容器中了。
          注意:我們的Person類上 就不需要任何的注解了,直接導(dǎo)入即可。

          3.2 @Import + ImportSelector

          其實(shí)在@Import注解的源碼中,說(shuō)的已經(jīng)很清楚了,感興趣的可以看下,我們實(shí)現(xiàn)一個(gè)ImportSelector的接口,然后實(shí)現(xiàn)其中的方法,進(jìn)行導(dǎo)入。
          代碼如下:

          @Import(MyImportSelector.class)public class Demo1 {     public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }} class MyImportSelector implements ImportSelector {    @Override    public String[] selectImports(AnnotationMetadata importingClassMetadata) {        return new String[]{"com.springboot.pojo.Person"};    }}

          我自定義了一個(gè) MyImportSelector 實(shí)現(xiàn)了 ImportSelector 接口,重寫selectImports 方法,然后將我們要導(dǎo)入的類的全限定名寫在里面即可,實(shí)現(xiàn)起來(lái)也是非常簡(jiǎn)單。

          3.3 @Import + ImportBeanDefinitionRegistrar

          這種方式也需要我們實(shí)現(xiàn) ImportBeanDefinitionRegistrar 接口中的方法,具體代碼如下:

          @Import(MyImportBeanDefinitionRegistrar.class)public class Demo1 {     public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }} class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {     @Override    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {        // 構(gòu)建一個(gè)beanDefinition, 關(guān)于beanDefinition我后續(xù)會(huì)介紹,可以簡(jiǎn)單理解為bean的定義.        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();        // 將beanDefinition注冊(cè)到Ioc容器中.        registry.registerBeanDefinition("person", beanDefinition);    }}

          上述實(shí)現(xiàn)其實(shí)和Import的第二種方式差不多,都需要去實(shí)現(xiàn)接口,然后進(jìn)行導(dǎo)入。接觸到了一個(gè)新的概念,BeanDefinition,可以簡(jiǎn)單理解為bean的定義(bean的元數(shù)據(jù)),也是需要放在IOC容器中進(jìn)行管理的,先有bean的元數(shù)據(jù),applicationContext再根據(jù)bean的元數(shù)據(jù)去創(chuàng)建Bean。

          3.4 @Import + DeferredImportSelector

          這種方式也需要我們進(jìn)行實(shí)現(xiàn)接口,其實(shí)它和@Import的第二種方式差不多,DeferredImportSelector 它是 ImportSelector 的子接口,所以實(shí)現(xiàn)的方法和第二種無(wú)異。只是Spring的處理方式不同,它和Spring Boot中的自動(dòng)導(dǎo)入配置文件 延遲導(dǎo)入有關(guān),非常重要。使用方式如下:

          @Import(MyDeferredImportSelector.class)public class Demo1 {    public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }}class MyDeferredImportSelector implements DeferredImportSelector {    @Override    public String[] selectImports(AnnotationMetadata importingClassMetadata) {        // 也是直接將Person的全限定名放進(jìn)去        return new String[]{Person.class.getName()};    }}

          關(guān)于@Import注解的使用方式,大概就以上三種,當(dāng)然它還可以搭配@Configuration注解使用,用于導(dǎo)入一個(gè)配置類。

          4、使用FactoryBean接口

          FactoryBean接口和BeanFactory千萬(wàn)不要弄混了,從名字其實(shí)可以大概的區(qū)分開,F(xiàn)actoryBean, 后綴為bean,那么它其實(shí)就是一個(gè)bean, BeanFactory,顧名思義 bean工廠,它是IOC容器的頂級(jí)接口,這倆接口都很重要。
          代碼示例:

          @Configurationpublic class Demo1 {    @Bean    public PersonFactoryBean personFactoryBean() {        return new PersonFactoryBean();    }     public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo1.class);        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }} class PersonFactoryBean implements FactoryBean<Person{     /**     *  直接new出來(lái)Person進(jìn)行返回.     */    @Override    public Person getObject() throws Exception {        return new Person();    }    /**     *  指定返回bean的類型.     */    @Override    public Class<?> getObjectType() {        return Person.class;    }}

          上述代碼,我使用@Configuration + @Bean的方式將 PersonFactoryBean 加入到容器中,注意,我沒(méi)有向容器中注入 Person, 而是直接注入的 PersonFactoryBean 然后從容器中拿Person這個(gè)類型的bean,成功運(yùn)行。

          5、使用 BeanDefinitionRegistryPostProcessor

          其實(shí)這種方式也是利用到了 BeanDefinitionRegistry,在Spring容器啟動(dòng)的時(shí)候會(huì)執(zhí)行 BeanDefinitionRegistryPostProcessor 的 postProcessBeanDefinitionRegistry 方法,大概意思就是等beanDefinition加載完畢之后,對(duì)beanDefinition進(jìn)行后置處理,可以在此進(jìn)行調(diào)整IOC容器中的beanDefinition,從而干擾到后面進(jìn)行初始化bean。
          具體代碼如下:

          public class Demo1 {    public static void main(String[] args) {        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();        MyBeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new MyBeanDefinitionRegistryPostProcessor();        applicationContext.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor);        applicationContext.refresh();        Person bean = applicationContext.getBean(Person.class);        System.out.println(bean);    }} class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {     @Override    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();        registry.registerBeanDefinition("person", beanDefinition);    }    @Override    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {     }}

          上述代碼中,我們手動(dòng)向beanDefinitionRegistry中注冊(cè)了person的BeanDefinition。最終成功將person加入到applicationContext中,上述的幾種方式的具體原理,我后面會(huì)進(jìn)行介紹。

          小結(jié)

          向spring容器中加入bean的幾種方式:
          • @Configuration + @Bean
          • @ComponentScan + @Component
          • @Import 配合接口進(jìn)行導(dǎo)入
          • 使用FactoryBean。
          • 實(shí)現(xiàn)BeanDefinitionRegistryPostProcessor進(jìn)行后置處理。
          <END>

          推薦閱讀:

          不到 20 人的 IT 公司該去嗎?

          面試 Dubbo ,卻問(wèn)我和 SpringCloud 有什么區(qū)別?

             
             
          互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G)

          內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬(wàn)并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!

          ?戳閱讀原文領(lǐng)取!                                  朕已閱 

          瀏覽 471
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆成人AV一二三区在线播放 | 福利视频免费观看 | 久操久操| 91精品国产91久久久久久 | 一级性爱视频中文字幕 |