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

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

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

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注解導(dǎo)入

@import注解源碼

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @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)入類(lèi)

然后自動(dòng)的就被放置在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導(dǎo)入person類(lèi),然后嘗試從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容器中進(jìn)行管理的,先有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) {
        // 構(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);
    }
}

3.4 @Import + DeferredImportSelector

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()};
    }
}

4、使用FactoryBean接口

FactoryBean, 后綴為bean,那么它其實(shí)就是一個(gè)bean,

BeanFactory,顧名思義 bean工廠(chǎng),它是IOC容器的頂級(jí)接口

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

5、使用 BeanDefinitionRegistryPostProcessor

等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 {
 
    }
}

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

相關(guān)文章

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

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

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

    java如何強(qiáng)制刪除java程序占用的文件

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

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

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

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

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

    淺談Java double 相乘的結(jié)果偏差小問(wèn)題

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

    解決SpringBoot中LocalDateTime返回前端數(shù)據(jù)為數(shù)組結(jié)構(gòu)的問(wèn)題

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

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

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

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

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

    在idea中設(shè)置項(xiàng)目編碼格式為UTF-8的操作方法

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

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

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

最新評(píng)論