日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Configuration源碼,你了解多少?

來(lái)源: 責(zé)編: 時(shí)間:2023-10-25 15:48:49 321觀看
導(dǎo)讀Configuration最近看源碼時(shí),經(jīng)常看了下@Configuration(proxyBeanMethods = false)這樣的配置,但從命名上看應(yīng)該是與代理有關(guān)的,于是抽個(gè)時(shí)間了解了下proxyBeanMethods首先這個(gè)是@Configuration注解中的一個(gè)參數(shù),我們都知

Configuration

最近看源碼時(shí),經(jīng)常看了下@Configuration(proxyBeanMethods = false)這樣的配置,但從命名上看應(yīng)該是與代理有關(guān)的,于是抽個(gè)時(shí)間了解了下mv628資訊網(wǎng)——每日最新資訊28at.com

proxyBeanMethods

首先這個(gè)是@Configuration注解中的一個(gè)參數(shù),我們都知道,@Configuration是Spring中的配置類,一般用來(lái)申明Bean,在默認(rèn)情況下proxyBeanMethods為truemv628資訊網(wǎng)——每日最新資訊28at.com

含義

從源碼中可以看到對(duì)該參數(shù)的描述如下:mv628資訊網(wǎng)——每日最新資訊28at.com

Specify whether @Bean methods should get proxied in order to enforce bean lifecycle behavior, e.g. to return shared singleton bean instances even in case of direct @Bean method calls in user code. This feature requires method interception, implemented through a runtime-generated CGLIB subclass which comes with limitations such as the configuration class and its methods not being allowed to declare final.The default is true, allowing for 'inter-bean references' via direct method calls within the configuration class as well as for external calls to this configuration's @Bean methods, e.g. from another configuration class. If this is not needed since each of this particular configuration's @Bean methods is self-contained and designed as a plain factory method for container use, switch this flag to false in order to avoid CGLIB subclass processing.Turning off bean method interception effectively processes @Bean methods individually like when declared on non-@Configuration classes, a.k.a. "@Bean Lite Mode" (see @Bean's javadoc). It is therefore behaviorally equivalent to removing the @Configuration stereotype.
  1. 該屬性的作用是指定標(biāo)注了@Bean的方法在執(zhí)行生命周期的時(shí)候是否應(yīng)該被代理。比如在代碼中直接調(diào)用@Bean標(biāo)注的方法時(shí)要返回共享的單例Bean實(shí)例(關(guān)閉代理之后不會(huì)返回共享的Bean實(shí)例)。
  2. 該特性是通過(guò)運(yùn)行時(shí)CGLIB子類 實(shí)現(xiàn)的方法攔截。該子類有一些限制,比如配置類和它的方法不允許聲明為final類型。
  3. 默認(rèn)為true,即允許 “inter-bean references” 通過(guò)配置類內(nèi)部的直接方法調(diào)用,也可以通過(guò)外部調(diào)用該配置類的@Bean標(biāo)注的方法。如果該配置類是一個(gè)特殊的配置類:每一個(gè)@Bean標(biāo)注的方法 都是自包含的,并設(shè)計(jì)為供容器使用的普通工廠方法,可以設(shè)置該屬性為false,以避免CGLIB子類處理。
  4. 關(guān)閉Bean方法攔截可以高效的單獨(dú)處理@Bean方法,就像聲明在一個(gè)non-@Configuration類上一樣,@Bean Lite Mode。這種行為類似于刪除@Configuration原型。

當(dāng)直接在Configuration中直接通過(guò)方法,實(shí)現(xiàn)實(shí)例件的屬性依賴時(shí),IDEA會(huì)有這樣一段提示:mv628資訊網(wǎng)——每日最新資訊28at.com

Method annotated with @Bean is called directly in a @Configuration where proxyBeanMethods set to false. Set proxyBeanMethods to true or use dependency injection.

示例

先通過(guò)下面的示例看下現(xiàn)象:mv628資訊網(wǎng)——每日最新資訊28at.com

兩個(gè)配置類,寫法差不多,區(qū)別在與proxyBeanMethods的配置以及AnimalCage屬性的注入方法。mv628資訊網(wǎng)——每日最新資訊28at.com

@Configuration(proxyBeanMethods = false)public class GenericConfiguration {        @Bean    public DogCage dogCage(){        return new DogCage();    }    @Bean    public AnimalCage animalEden(){        AnimalCage animalCage = new AnimalCage();        animalCage.addCage(dogCage());        return animalCage;    }}@Configuration(proxyBeanMethods = true)public class ProxyConfiguration {    @Bean    public DogCage dogCage(){        return new DogCage();    }    @Bean    public AnimalCage animalEden(@Autowired List<Cage> cages){        return new AnimalCage(cages);    }}

先看下GenericConfiguration配置的情況:mv628資訊網(wǎng)——每日最新資訊28at.com

public class Tests {    @Autowired    private BeanFactory beanFactory;    @Autowired    private GenericConfiguration genericConfiguration;    @Autowired    private AnimalCage animalCage;    @Autowired    private DogCage dogCage;    @Test    public void runConfig() {        log.info("configuration: {}", genericConfiguration); // 原始對(duì)象類型        log.info("Configuration中的Bean: {}", genericConfiguration.dogCage() == genericConfiguration.dogCage()); // 兩次結(jié)果不一樣        log.info("容器中的Bnea: {}", beanFactory.getBean(DogCage.class) == beanFactory.getBean(DogCage.class));// 從Spring容器中取值都是一樣的        animalCage.cages.forEach(cage -> {            if (cage instanceof DogCage) {                log.info("dogCage : {} ", cage == dogCage); // 和上面的對(duì)象不一致,非單例            }        });    }}

再看下ProxyConfiguration配置的情況:mv628資訊網(wǎng)——每日最新資訊28at.com

public class Tests {    @Test    public void runConfig() {        log.info("configuration: {}", proxyConfiguration); // 1、CGLIB代理的對(duì)象        log.info("Configuration中的Bean: {}", proxyConfiguration.dogCage() == proxyConfiguration.dogCage()); // 2、兩次結(jié)果相同        log.info("容器中的Bnea: {}", beanFactory.getBean(DogCage.class) == beanFactory.getBean(DogCage.class));// 3、從Spring容器中取值都是一樣的        animalCage.cages.forEach(cage -> {            if (cage instanceof DogCage) {                log.info("dogCage : {} ", cage == dogCage); // 和上面的對(duì)象不一致,非單例            }        });    }}

會(huì)得到這樣的現(xiàn)象:mv628資訊網(wǎng)——每日最新資訊28at.com

  1. proxyBeanMethods = true時(shí),從Spring容器中取出的Configuration是一個(gè)Cglib代理配置,否則是一個(gè)原始類型配置
  2. proxyBeanMethods = true時(shí),多次調(diào)用Bean方法,每次都是一個(gè)新對(duì)象,否則都是同一個(gè)對(duì)象
  3. 從Spring容器中取出Bean,不管多少次,都是同一個(gè)對(duì)象,也就是單例的

Lite Full Mode

看到上面的現(xiàn)象后,我們有必要了解下Spring配置中的Lite和Full兩種模式mv628資訊網(wǎng)——每日最新資訊28at.com

lite模式包含:mv628資訊網(wǎng)——每日最新資訊28at.com

  • 被@Component修飾的類
  • 通過(guò)@ComponentScan掃描的類
  • 通過(guò)@Import導(dǎo)入的配置類
  • 通過(guò)@ImportResource導(dǎo)入的Spring配置文件
  • 沒(méi)有任何Spring相關(guān)注解,類里面有@Bean修飾的方法
  • 被@Configuration修飾,但proxyBeanMethods = false

full模式包含:mv628資訊網(wǎng)——每日最新資訊28at.com

  • 被@Configuration修飾,且屬性proxyBeanMethods = true(默認(rèn))

full模式使用特性:mv628資訊網(wǎng)——每日最新資訊28at.com

  • full模式下的配置類會(huì)被Cglib代理生成代理類取代原始類型保存到在容器中
  • full模式下的@Bean方法不能是private和final,因?yàn)榉椒〞?huì)被重寫
  • 單例scope下不同@Bean方法可以互相引用,實(shí)現(xiàn)單實(shí)例的語(yǔ)義

lite模式使用特性:mv628資訊網(wǎng)——每日最新資訊28at.com

  • lite模式下的配置類不生成代理,原始類型進(jìn)入容器
  • lite模式下的@Bean方法可以是private和final
  • 單例scope下不同@Bean方法引用時(shí)無(wú)法做到單例,通過(guò)@Bean方法生成的對(duì)象都是新的實(shí)例

結(jié)束語(yǔ)

@Configuration(proxyBeanMethods = false)的配置其實(shí)是Lite模式,這種模式下,配置類不會(huì)生成代理類,速度會(huì)更快,但是要注意,在配置類中的@Bean方法,不能用來(lái)實(shí)現(xiàn)單例級(jí)別的依賴。mv628資訊網(wǎng)——每日最新資訊28at.com


mv628資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-14814-0.htmlConfiguration源碼,你了解多少?

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 把握效率與最優(yōu)性:Dijkstra算法的探索

下一篇: DISC-FinLLM:復(fù)旦大學(xué)團(tuán)隊(duì)發(fā)布中文智慧金融系統(tǒng),采用多專家微調(diào)框架

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 连云港市| 虎林市| 涪陵区| 北宁市| 湖口县| 潍坊市| 缙云县| 昌图县| 连城县| 乌拉特前旗| 安吉县| 张掖市| 茌平县| 三河市| 新郑市| 灌阳县| 香港| 衡阳市| 武邑县| 乾安县| 江陵县| 西宁市| 日喀则市| 本溪| 临沂市| 望江县| 新余市| 嘉兴市| 东海县| 葫芦岛市| 根河市| 曲阳县| 翁牛特旗| 灵武市| 海丰县| 滨州市| 正宁县| 黑河市| 云林县| 都昌县| 合作市|