国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[Spring6.0源碼解析]簡(jiǎn)述@Configuration注解

這篇具有很好參考價(jià)值的文章主要介紹了[Spring6.0源碼解析]簡(jiǎn)述@Configuration注解。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

@Configuration 標(biāo)注在類上,啟動(dòng) Spring 會(huì)自動(dòng)掃描@Configuration注解的類,將其注冊(cè)到IOC容器并實(shí)例化bean對(duì)象。如果在@Configuration注解的類中使用@Bean注解某個(gè)類對(duì)象的方法,Spring也會(huì)自動(dòng)將注解了@Bean的方法注冊(cè)到IOC容器,并進(jìn)行實(shí)例化。

注解源碼

@Configuration 注解本質(zhì)上是個(gè) @Component 注解,所以被 @Configuration 標(biāo)注的類會(huì)被注冊(cè)到IOC,且可以被 @ComponentScan 注解掃描到。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

	/**
	 * 存入到Spring IOC容器中的ID
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

	/**
	 * 表示被@Configuration注解的類是否被代理,以及配置類中被@Bean注解的方法生成的Bean
     * 在IOC容器中是否為單例對(duì)象
     * 
     * true:full全局模式(默認(rèn))
     * false:lite輕量級(jí)模式
     * 
     * full全局模式,被@Configuration注解的配置類會(huì)被代理(CGLIB實(shí)現(xiàn)),配置類中被@Bean
     * 注解的方法生成的Bean在IOC容器中是單例模式。也就是說,無論調(diào)用多少次被@Bean標(biāo)注的
     * 方法,返回的都是同一個(gè)bean對(duì)象。
     * 
     * lite輕量級(jí)模式,被@Configuration注解的配置類不會(huì)被代理,配置類中被@Bean注解的方法
     * 生成的Bean在IOC容器中也不是單例模式。也就是說,每次調(diào)用被@Bean注解標(biāo)注的方法時(shí),都會(huì)
     * 返回一個(gè)新的Bean對(duì)象。
     * 
	 * @since 5.2(Spring 5.2版本加入)
	 */
	boolean proxyBeanMethods() default true;

	/**
	 * 表示使用@Bean注解標(biāo)注的方法是否需要唯一的方法名。
	 * 
	 * true:使用@Bean注解標(biāo)注的方法具有唯一方法名稱,且方法名稱不會(huì)重疊
	 * false:使用@Bean注解標(biāo)注的方法不唯一,存在重疊風(fēng)險(xiǎn)
	 * 
	 * 默認(rèn)為true。
	 * 
	 * @since 6.0(Spring 6.0版本加入)
	 */
	boolean enforceUniqueMethods() default true;

}

使用場(chǎng)景

當(dāng)某個(gè)類被@Configuration注解標(biāo)注時(shí),說明這個(gè)類是配置類。可以在這個(gè)類中,使用@Bean注解,向IOC容器中注入Bean對(duì)象;也可以使用 @Autowrite、@Resource、@Inject等注解來注入所需要的Bean對(duì)象。

另外,在使用 AnnotationConfigApplicationContext 類創(chuàng)建IOC容器事,需要注意兩點(diǎn):

  1. 如果使用傳入 Class 入?yún)⒌臉?gòu)造函數(shù),則傳入Class的配置類上的 @Configuration 可以省略,但是如果省略 @Configuration ,每次調(diào)用配置類中被 @Bean 標(biāo)注的方法時(shí),都會(huì)返回不同的 Bean 實(shí)例對(duì)象。
  2. 如果使用傳入 String 入?yún)⒌臉?gòu)造函數(shù),表示傳入應(yīng)用程序的包名來創(chuàng)建 IOC容器,則配置類上的 @Configuration 不可以省略。

兩種構(gòu)造方法如下:

	/**
	 * Create a new AnnotationConfigApplicationContext, deriving bean definitions
	 * from the given component classes and automatically refreshing the context.
	 * @param componentClasses one or more component classes — for example,
	 * {@link Configuration @Configuration} classes
	 */
	public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
		this();
		register(componentClasses);
		refresh();
	}

	/**
	 * Create a new AnnotationConfigApplicationContext, scanning for components
	 * in the given packages, registering bean definitions for those components,
	 * and automatically refreshing the context.
	 * @param basePackages the packages to scan for component classes
	 */
	public AnnotationConfigApplicationContext(String... basePackages) {
		this();
		scan(basePackages);
		refresh();
	}

使用案例

準(zhǔn)備代碼

  • 一個(gè)用于注冊(cè)到IOC的類:
public class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 配置類
@Configuration
public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}
  • 啟動(dòng)類
public class ConfigurationAnnotationTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationAnnotationTest.class);
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigurationAnnotationConfig.class);
        ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
        Person person1 = config.person();
        Person person2 = config.person();
        LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
    }
}

proxyBeanMethods的使用

在之前提到,proxyBeanMethods配置表示用 @Bean 注解的方法在IOC容器中是否為單例對(duì)象,默認(rèn)為true。

默認(rèn)情況下,打印出結(jié)果如下:

person1 是否等于 person2 ===>> true

修改一下proxyBeanMethods的值為false:

@Configuration(proxyBeanMethods = false)
public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

打印結(jié)果如下:

person1 是否等于 person2 ===>> false

從輸出結(jié)果可以看出,當(dāng)@Configuration中的proxyBeanMethods屬性為false時(shí),每次調(diào)用@Configuration注解標(biāo)注類中被@Bean標(biāo)注的方法時(shí),都會(huì)返回不同的Bean實(shí)例對(duì)象。

創(chuàng)建IOC容器

傳入配置類

調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入配置類的Class對(duì)象創(chuàng)建IOC容器時(shí),可以省略配置類上的@Configuration注解,如下:

public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

輸出結(jié)果:

person1 是否等于 person2 ===>> false

可以看到,若省略配置類上的@Configuration注解,則每次調(diào)用配置類中被@Bean注解標(biāo)注的方法時(shí),都會(huì)返回不同的Bean實(shí)例對(duì)象,與@Configuration中設(shè)置proxyBeanMethods的屬性為false的效果相同。

傳入包

調(diào)用AnnotationConfigApplicationContext類的構(gòu)造方法傳入包名創(chuàng)建IOC容器時(shí),不能省略配置類上的@Configuration注解:

public class ConfigurationAnnotationConfig {
    @Bean
    public Person person(){
        return new Person();
    }
}

執(zhí)行函數(shù):

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("io.binghe.spring.annotation.chapter01.configuration");
        ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
        Person person1 = config.person();
        Person person2 = config.person();
        LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
    }

此時(shí)運(yùn)行main方法,會(huì)發(fā)生報(bào)錯(cuò):

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.binghe.spring.annotation.chapter01.configuration.config.ConfigurationAnnotationConfig' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
	at io.binghe.spring.annotation.chapter01.configuration.ConfigurationAnnotationTest.main(ConfigurationAnnotationTest.java:36)

添加上@Configuration注解則程序執(zhí)行正常。

擴(kuò)展知識(shí)

AnnotationConfigApplicationContext

Spring在 BeanFactory 的基礎(chǔ)上提供一些具體容器的實(shí)現(xiàn)。AnnotationConfigApplicationContext就是一個(gè)用來管理注解 Bean 的容器。如下結(jié)構(gòu)圖:

從圖中可以看到,AnnotationConfigApplicationContext繼承GenericApplicationContext(通用應(yīng)用上下文),而GenericApplicationContext又實(shí)現(xiàn)了BeanDefinitionRegistry接口,所以可以通過AnnotationConfigApplicationContext實(shí)例類注冊(cè)BeanDefintion,然后調(diào)用refresh()方法來初始化上下文。AnnotationConfigApplicationContext還繼承了AbstractApplicationContext,而AbstractApplicationContext提供了ApplicationContext的抽象實(shí)現(xiàn)文章來源地址http://www.zghlxwxcb.cn/news/detail-410037.html

到了這里,關(guān)于[Spring6.0源碼解析]簡(jiǎn)述@Configuration注解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Spring IOC @Configuration注解分析

    在使用SpringBoot開發(fā)時(shí),最常用的注解有@Component、@Service、@Controller、@Configuration等。當(dāng)類使用這些注解標(biāo)記時(shí),類會(huì)被Spring IOC容器管理,包括創(chuàng)建,填充屬性和實(shí)例化。 但是Spring容器如何發(fā)現(xiàn)并將這些類放到容器進(jìn)行管理呢? 今天這篇博客主要分析Spring如何處理@Configuratio

    2024年02月08日
    瀏覽(18)
  • Spring很常用的@Conditional注解的使用場(chǎng)景和源碼解析

    你好,我是劉牌! 今天要分享的是Spring的注解@Conditional,@Conditional是一個(gè)條件注解,它的作用是判斷Bean是否滿足條件,如果滿足條件,則將Bean注冊(cè)進(jìn)IOC中,如果不滿足條件,則不進(jìn)行注冊(cè),這個(gè)注解在SpringBoot中衍生出很多注解,比如 @ConditionalOnProperty , @ConditionalOnBean ,

    2023年04月14日
    瀏覽(24)
  • Spring6.0 源碼部署

    Spring6.0 源碼部署

    Git JDK17 Gradle(版本號(hào)需要和Spring源碼中的版本一致) 官網(wǎng)地址

    2024年02月16日
    瀏覽(57)
  • Spring6源碼編譯、安裝

    Spring6源碼編譯、安裝

    名稱 版本 jdk 17 gradle 8.1.1 spring源碼版本 6.0.9 下載并解壓 配置環(huán)境變量 驗(yàn)證 下載并解壓 配置環(huán)境變量 驗(yàn)證 配置鏡像倉(cāng)庫(kù) spring在Github官網(wǎng)上有詳細(xì)的說明文檔,可以參考,但按照步驟做輝有些報(bào)錯(cuò)??,還是按照自己總結(jié)的步驟來吧。 github源碼地址 國(guó)內(nèi)gitcode鏡像 官方構(gòu)建

    2024年02月08日
    瀏覽(17)
  • Spring5學(xué)習(xí)隨筆-高級(jí)注解(@ComponentScan、@Configuration.....),替換XML配置文件

    Spring5學(xué)習(xí)隨筆-高級(jí)注解(@ComponentScan、@Configuration.....),替換XML配置文件

    學(xué)習(xí)視頻:【孫哥說Spring5:從設(shè)計(jì)模式到基本應(yīng)用到應(yīng)用級(jí)底層分析,一次深入淺出的Spring全探索。學(xué)不會(huì)Spring?只因你未遇見孫哥】 Spring在3.x提供的新的注解,用于替換XML配置文件。 問題 : 配置Bean在應(yīng)用的過程中替換了XML具體的什么內(nèi)容? AnnotationConfigApplicationContext 配

    2024年02月05日
    瀏覽(20)
  • spring6.0.x源碼調(diào)試環(huán)境搭建

    搭建一個(gè)spring源碼調(diào)試環(huán)境,創(chuàng)建一個(gè)spring-demo模塊,寫一些測(cè)試代碼。 給源碼添加注釋。 給源碼打包 Spring Framework fork到自己的GitHub倉(cāng)庫(kù),然后拉代碼 下載 https://gradle.org/releases/ gradle/wrapper/gradle-wrapper.properties 找到版本為7.6的 gradle 找到 7.6 的版本,點(diǎn)擊下載(注:點(diǎn)擊之后

    2023年04月09日
    瀏覽(21)
  • spring復(fù)習(xí):(50)@Configuration注解配置的singleton的bean是什么時(shí)候被創(chuàng)建出來并緩存到容器的?

    spring復(fù)習(xí):(50)@Configuration注解配置的singleton的bean是什么時(shí)候被創(chuàng)建出來并緩存到容器的?

    一、主類: 二、配置類: 三、singleton bean的創(chuàng)建流程 運(yùn)行到context.refresh(); 進(jìn)入refresh方法: 向下運(yùn)行到紅線位置時(shí): 會(huì)實(shí)例化所有的singleton bean.進(jìn)入finisheBeanFactoryInitialization方法: 向下拖動(dòng)代碼,可以看到beanFactory.preInstantiateSingletons(); 進(jìn)入preInstantiateSingletons方法: 可以看

    2024年02月16日
    瀏覽(23)
  • 動(dòng)力節(jié)點(diǎn)|Spring6框架學(xué)習(xí)教程,從基礎(chǔ)到手撕源碼一套打通

    動(dòng)力節(jié)點(diǎn)|Spring6框架學(xué)習(xí)教程,從基礎(chǔ)到手撕源碼一套打通

    Spring框架已廣泛應(yīng)用于諸多Java應(yīng)用程序的開發(fā)中,它提供了很多解決方案及最佳實(shí)踐,簡(jiǎn)化了Java應(yīng)用程序的開發(fā)過程并加速了開發(fā)。 Spring6.0版本是下一個(gè)十年的新開端,動(dòng)力節(jié)點(diǎn)老杜精心打造全新升級(jí)版Spring6教程,手把手教學(xué),帶大家從小白 蛻變 成為技術(shù)大牛 讓 初學(xué)者

    2024年02月13日
    瀏覽(22)
  • 【Spring Boot】自動(dòng)配置及重要注解解析

    【Spring Boot】自動(dòng)配置及重要注解解析

    1、springboot自動(dòng)配置了哪些功能 1)tomcat的自動(dòng)配置 引入了tomcat的相關(guān)依賴 2)自動(dòng)配置springmvc及常用功能 我們?cè)谥鲉?dòng)類中加兩行代碼,查看一下spring boot加載了哪些組件 通過在控制臺(tái)輸出中搜索,我們可以發(fā)現(xiàn)轉(zhuǎn)發(fā)請(qǐng)求的dispatcherServlet、解決亂碼的characterEncodingFilter還有視

    2023年04月09日
    瀏覽(26)
  • 深入解析 Spring Framework 中 @Autowired 注解的實(shí)現(xiàn)原理

    深入解析 Spring Framework 中 @Autowired 注解的實(shí)現(xiàn)原理

    關(guān)于@Autowired注解的作用 @Autowired 注解在Spring中的作用是實(shí)現(xiàn)依賴注入(Dependency Injection),它用于自動(dòng)裝配(autowiring)Spring Bean 的依賴關(guān)系。具體來說, @Autowired 注解有以下作用: 自動(dòng)裝配依賴 :通過在類的字段、構(gòu)造函數(shù)、方法參數(shù)等地方使用 @Autowired 注解,Spring 容器會(huì)

    2024年02月08日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包