Spring 容器初始化:@Configuration 注解的使用詳解

先來簡單介紹一下@Configuration是用來干嘛的:

@Configuration 用于定義配置類,被注解的類內(nèi)部包含有一個(gè)或多個(gè)被@Bean注解的方法,這些方法將會(huì)被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進(jìn)行掃描,并用于構(gòu)建bean定義,初始化Spring容器。

來看代碼中如何使用吧,舉栗子啦:

一 、@Configuration配置spring并啟動(dòng)spring容器

啟動(dòng)類

@SpringBootApplication
public class SpirngdemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SpirngdemoApplication.class, args);

        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        System.out.println("..................Service starts successfully..................");
    }

}

配置類

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器初始化...");
    }
}

輸出結(jié)果:

TestConfiguration容器初始化...
..................Service starts successfully..................

從輸出結(jié)果的先后順序可以看出,Spring 在啟動(dòng)過程中是先進(jìn)行容器的初始化操作的。

二、@Configuration啟動(dòng)容器+@Bean注冊Bean,@Bean下管理bean的生命周期

啟動(dòng)類

@SpringBootApplication
public class SpirngdemoApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SpirngdemoApplication.class, args);

        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        System.out.println("..................Service starts successfully..................");
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }

}

bean 類

public class TestBean {

    private String username;
    private String url;
    private String password;

    public void sayHello() {
        System.out.println("TestBean sayHello...");
    }

    public String toString() {
        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
    }

    public void start() {
        System.out.println("TestBean 初始化。。。");
    }

    public void cleanUp() {
        System.out.println("TestBean 銷毀。。。");
    }
}

配置類

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器初始化...");
    }

    @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")
    @Scope(value = "prototype")
    public TestBean testBean() {
        return new TestBean();
    }
}
順便介紹下@scope注解

Scope,也稱作用域,在 Spring IoC 容器是指其創(chuàng)建的 Bean 對(duì)象相對(duì)于其他 Bean 對(duì)象的請求可見范圍。在 Spring IoC 容器中具有以下幾種作用域:基本作用域(singleton、prototype),Web 作用域(reqeust、session、globalsession),自定義作用域。

  • singleton:單例模式,在整個(gè)Spring IoC容器中,使用singleton定義的Bean將只有一個(gè)實(shí)例

  • prototype:原型模式,每次通過容器的getBean方法獲取prototype定義的Bean時(shí),都將產(chǎn)生一個(gè)新的Bean實(shí)例

  • request:對(duì)于每次HTTP請求,使用request定義的Bean都將產(chǎn)生一個(gè)新實(shí)例,即每次HTTP請求將會(huì)產(chǎn)生不同的Bean實(shí)例。只有在Web應(yīng)用中使用Spring時(shí),該作用域才有效

  • session:對(duì)于每次HTTP Session,使用session定義的Bean都將產(chǎn)生一個(gè)新實(shí)例。同樣只有在Web應(yīng)用中使用Spring時(shí),該作用域才有效

  • globalsession:每個(gè)全局的HTTP Session,使用session定義的Bean都將產(chǎn)生一個(gè)新實(shí)例。典型情況下,僅在使用portlet context的時(shí)候有效。同樣只有在Web應(yīng)用中使用Spring時(shí),該作用域才有效

輸出結(jié)果

TestConfiguration容器初始化...
..................Service starts successfully..................
TestBean 初始化。。。
TestBean sayHello...

@Bean注解注冊bean,同時(shí)可以指定初始化和銷毀方法
可以看到@Bean也可以管理bean 的生命周期

三、@Configuration啟動(dòng)容器+@Component注冊Bean

啟動(dòng)類同上

配置類:

@Configuration
@ComponentScan(basePackages = "com.wenxuan.springdemo.bean")
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器初始化...");
    }
}

bean 類:

@Component
public class TestBean {

    private String username;
    private String url;
    private String password;

    public void sayHello() {
        System.out.println("TestBean sayHello...");
    }

    public String toString() {
        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
    }

    public void start() {
        System.out.println("TestBean 初始化。。。");
    }

    public void cleanUp() {
        System.out.println("TestBean 銷毀。。。");
    }
}

bean 類添加@Component注解,配置類不再添加@Bean 手動(dòng)聲明,而是添加@ComponentScan 注解,并指明配置的包,同樣達(dá)到初始化 bean 的目的。
輸出結(jié)果:

TestConfiguration容器初始化...
..................Service starts successfully..................
TestBean sayHello...
寫在后面的話:

以上代碼都是自己測試過的,紙上得來終覺淺,不妨自己照著上面的過程自己試驗(yàn)一下,相信你會(huì)有更深的體會(huì)。

在此也想感謝一下這篇文章,給了我參考:
https://www.cnblogs.com/duanxz/p/7493276.html

官網(wǎng)文檔:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容