一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - @Bean注解和@Configuration、@Component注解組合使用的區別

@Bean注解和@Configuration、@Component注解組合使用的區別

2022-03-09 00:38靈穎橋人 Java教程

這篇文章主要介紹了@Bean注解和@Configuration、@Component注解組合使用的區別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、@Bean的“full”模式和“lite”模式

在一般常見情況下,@Bean注解在@Configuration類中聲明,稱之為“full”模式;當@Bean注解和@Component注解組合使用時,稱之為“lite”模式。

這兩種組合使用的情況,初次看文檔時沒看明白,多看了幾次又跑了測試代碼,才大致理解了區別。

二、兩種模式的差異

如果只是把@Bean注解用在方法上,并且各個@Bean注解的方法之間沒有調用,上述兩種模式達到的效果基本相同,都可以把@Bean注解方法返回的對象作為bean注冊到容器中。

如果各個@Bean注解的方法之間有相互調用,那么兩種模式就會有很大的區別-與full模式下的@Configuration不同,lite模式下 @Bean方法互相調用無法聲明Bean之間的依賴關系。

這點在@Bean注解源碼注釋上也有說明。

1、“full”模式下@Bean方法互相調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; 
@Configuration
public class Config {
 
    @Bean()
    public C c(){
        return new C();
    }
 
    @Bean
    public B b(){
        return new B(c());
    }
}

類B:

?
1
2
3
4
5
6
7
public class B {
    public C c;
    public B(C a){this.c=a;}
    public void shutdown(){
        System.out.println("b close...");
    }
}

 

類C:

?
1
2
3
4
5
6
7
8
9
10
11
12
public class C {
    private String name;
    @PostConstruct
    public void init(){
        this.name = "I am a bean";
    }
 
    @Override
    public String toString(){
        return "c say:" + name;
    }
}

如上述所示代碼,我們有兩個類B和C,在@Configuration類中,使用兩個@Bean方法分別定義bean b 和bean c,但是需要注意的是new B(c()) 所在的@Bean方法調用了另一個@Bean方法。

測試主程序類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.dxc.opentalk.springtest.config.B;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
@EnableAspectJAutoProxy
@ComponentScan("com.dxc.opentalk.springtest")
public class BootStrap {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext
                = new AnnotationConfigApplicationContext(BootStrap.class);
        B b = (B)applicationContext.getBean("b");
        System.out.println(b.c);
        System.out.println(applicationContext.getBean("c"));
        System.out.println(applicationContext.getBean("c").equals(b.c));
    }
}

程序輸出結果:

c say:I am a bean
c say:I am a bean
true

可以看出bean c 被注入到了bean b 中,bean b中的成員 c 確實是Spring容器中的bean。(其實,debug程序跟蹤Spring中的單例池更清晰,這里采用輸出結果說明)

2、“lite”模式下@Bean方法互相調用

還是上面的代碼,我們只把Config類上的注解更換成@Component,其余代碼保持不變:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; 
@Component
public class Config {
 
    @Bean()
    public C c(){
        return new C();
    }
 
    @Bean
    public B b(){
        return new B(c());
    }
}

再看一下測試主程序類輸出結果:

c say:null
c say:I am a bean
false

可以看到bean b中的c只是一個普通的c對象,并不是Spring容器中的bean(b中的c沒有執行bean的初始化回調方法也和單例池中的c bean不相等)。所以這種模式下,@Bean方法互相調用不能完成bean之間相互依賴關系的注入。

三、總結

綜上所述,在使用@Bean注解時需要注意兩種模式的區別,一般情況下@Bean都是和@Configuration注解搭配使用的。

但是@Configuration注解的類會被Spring使用CGLIB子類化,所以@Configuration注解的類不能用 final 修飾,而@Component注解沒有此限制。

如果使用@Bean注解和@Component注解組合需要完成bean之間相互依賴注入的話,官方推薦采用構造方法或者方法級別的依賴注入,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
 
@Component
public final class Config {
 
    @Bean()
    public C c(){
        return new C();
    }
    @Bean
    public B b(C c){//構造方法注入
        return new B(c);
    }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_22076345/article/details/104687011

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本中文字幕一区二区高清在线 | 亚洲精品短视频 | 日韩免费观看成第15集 | 逼逼狗影院 | 天天色踪合合 | 精品国产免费久久久久久婷婷 | 国产大秀视频一区二区三区 | 91视频完整版 | 无遮免费网站在线入口 | 亚洲香蕉网久久综合影院3p | 国产玖玖在线 | 青草国产| freefron性中国| 国产视频福利 | 欧美日韩中文国产一区二区三区 | 视频在线播放 | 国产一卡 | 男人天堂日韩 | 女人肮脏的交易中文字幕未删减版 | 99免费视频| 亚洲日韩精品欧美一区二区一 | 外国xxx| 国产欧美日韩专区 | 欧美成人中文字幕在线看 | 亚洲免费一 | 女主被男主为催奶药h | 娇妻与老头绿文小说系列 | 欧美日韩精品一区二区三区视频播放 | 国语在线| 亚洲欧美日韩另类在线 | 45分钟做受片免费观看 | 午夜精品国产自在现线拍 | 免费观看日本 | 亚洲 色 欧美 爱 视频 日韩 | 国产99视频精品免视看9 | 免费观看一级特黄三大片视频 | 日本一本二本三区免费 | 91.prom在线观看国产 | 色综合久久天天综合观看 | 哇嘎在线精品视频在线观看 | 美女被草出水 |