欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot將Bean放入容器的五種方式

 更新時間:2024年02月20日 10:46:30   作者:宣晨光  
這篇文章給大家介紹了SpringBoot將Bean放入容器的五種方式,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下

1、@Configuration + @Bean

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

2、@Componet + @ComponentScan

@Component
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 + '\'' +
                '}';
    }
}
 
@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);
    }
}

3、@Import注解導入

@import注解源碼

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

3.1、直接使用@import注解導入類

然后自動的就被放置在IOC容器中了。

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導入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);
    }
}

3.2 @Import + ImportSelector

@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"};
    }
}

3.3 @Import + ImportBeanDefinitionRegistrar

bean的定義(bean的元數(shù)據(jù)),也是需要放在IOC容器中進行管理的,先有bean的元數(shù)據(jù),

applicationContext再根據(jù)bean的元數(shù)據(jù)去創(chuàng)建Bean。

@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) {
        // 構建一個beanDefinition, 關于beanDefinition我后續(xù)會介紹,可以簡單理解為bean的定義.
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
        // 將beanDefinition注冊到Ioc容器中.
        registry.registerBeanDefinition("person", beanDefinition);
    }
}

3.4 @Import + DeferredImportSelector

DeferredImportSelector 它是 ImportSelector 的子接口,所以實現(xiàn)的方法和第二種無異。

只是Spring的處理方式不同,它和Spring Boot中的自動導入配置文件 延遲導入有關

@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的全限定名放進去
        return new String[]{Person.class.getName()};
    }
}

4、使用FactoryBean接口

FactoryBean, 后綴為bean,那么它其實就是一個bean,

BeanFactory,顧名思義 bean工廠,它是IOC容器的頂級接口

@Configuration
public 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出來Person進行返回.     */
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }
    /**     *  指定返回bean的類型.     */
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }
}

5、使用 BeanDefinitionRegistryPostProcessor

等beanDefinition加載完畢之后,對beanDefinition進行后置處理,

可以在此進行調(diào)整IOC容器中的beanDefinition,從而干擾到后面進行初始化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 {
 
    }
}

以上就是SpringBoot將Bean放入容器的五種方式的詳細內(nèi)容,更多關于SpringBoot Bean放入容器的資料請關注腳本之家其它相關文章!

相關文章

  • springboot搭建訪客管理系統(tǒng)的實現(xiàn)示例

    springboot搭建訪客管理系統(tǒng)的實現(xiàn)示例

    這篇文章主要介紹了springboot搭建訪客管理系統(tǒng)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • java如何強制刪除java程序占用的文件

    java如何強制刪除java程序占用的文件

    這篇文章主要介紹了java如何強制刪除java程序占用的文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • springboot調(diào)用HTML文件注意事項及說明

    springboot調(diào)用HTML文件注意事項及說明

    這篇文章主要介紹了springboot調(diào)用HTML文件注意事項及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • spring boot配置ssl實現(xiàn)HTTPS的方法

    spring boot配置ssl實現(xiàn)HTTPS的方法

    這篇文章主要介紹了spring boot配置ssl實現(xiàn)HTTPS的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 淺談Java double 相乘的結果偏差小問題

    淺談Java double 相乘的結果偏差小問題

    下面小編就為大家?guī)硪黄獪\談Java double 相乘的結果偏差小問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • 解決SpringBoot中LocalDateTime返回前端數(shù)據(jù)為數(shù)組結構的問題

    解決SpringBoot中LocalDateTime返回前端數(shù)據(jù)為數(shù)組結構的問題

    本文主要介紹了解決SpringBoot中LocalDateTime返回前端數(shù)據(jù)為數(shù)組結構的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-03-03
  • SpringCloud Gateway使用redis實現(xiàn)動態(tài)路由的方法

    SpringCloud Gateway使用redis實現(xiàn)動態(tài)路由的方法

    這篇文章主要介紹了SpringCloud Gateway使用redis實現(xiàn)動態(tài)路由的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • SpringAOP實現(xiàn)日志收集管理功能(步驟詳解)

    SpringAOP實現(xiàn)日志收集管理功能(步驟詳解)

    這篇文章主要介紹了SpringAOP實現(xiàn)日志收集管理功能,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 在idea中設置項目編碼格式為UTF-8的操作方法

    在idea中設置項目編碼格式為UTF-8的操作方法

    idea中的默認編碼為GBK,在開發(fā)過程中一般將編碼格式改為UTF-8,所以本文給大家介紹了在idea中設置項目編碼為UTF-8的操作方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • java內(nèi)存優(yōu)化的方法總結

    java內(nèi)存優(yōu)化的方法總結

    在本篇文章里小編給大家分享的是一篇關于java內(nèi)存優(yōu)化的方法總結內(nèi)容,有需要的朋友們可以學習下。
    2021-06-06

最新評論