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

SpringBoot中的@Configuration注解詳解

 更新時(shí)間:2023年08月04日 08:49:44   作者:cloneme01  
這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來完全代替XML 配置,JAVA配置就是通過 @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的,需要的朋友可以參考下

寫在前面

Spring Boot推薦使用JAVA配置來完全代替XML 配置,JAVA配置就是通過 @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的

也就是說:@Configuration+@Bean可以達(dá)到在Spring中使用XML配置文件的作用

@Configuration可理解為使用Spring框架時(shí)的XML文件中的<beans/>

@Bean可理解為使用Spring框架時(shí)XML里面的<bean/>標(biāo)簽。

使用目的

@Configuration用于定義配置類,可替換xml配置文件,被注解的類內(nèi)部包含有一個(gè)或多個(gè)被@Bean注解的方法

這些方法將會(huì)被AnnotationConfigWebApplicationContext或AnnotationConfigApplicationContext類進(jìn)行掃描,并構(gòu)建Bean定義,初始化Spring容器。

使用約束

  • @Configuration注解作用在類、接口(包含注解)上
  • @Configuration用于定義配置類,可替換XML配置文件,相當(dāng)于XML形式的Spring配置
  • @Configuration注解類中可以聲明一個(gè)或多個(gè) @Bean 注解的方法
  • @Configuration注解作用的類不能是 final 類型
  • @Configuration不可以是匿名類;
  • 嵌套的 @Configuration類必須是 static 的

代碼示例

package com.hadoopx.mallx.data.config.redis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.time.Duration;
@Configuration
public class RedisAutoConfig {
    @Bean
    public LettuceConnectionFactory defaultLettuceConnectionFactory(RedisStandaloneConfiguration defaultRedisConfig, GenericObjectPoolConfig defaultPoolConfig) {
        LettuceClientConfiguration clientConfig =
                LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(5000))
                        .poolConfig(defaultPoolConfig).build();
        return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
    }
    @Bean
    public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory defaultLettuceConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(defaultLettuceConnectionFactory);
        return stringRedisTemplate;
    }
    @Configuration
    public static class DefaultRedisConfig {
        /**
         * 獲取配置文件中spring.redis.host的值, 如果沒有則使用默認(rèn)值: 127.0.0.1
         * 使用如下:
         * @Value("${YML-KEY:DEFAULT-VALUE}")
         */
        @Value("${spring.redis.host:127.0.0.1}")
        private String host;
        @Value("${spring.redis.port:6379}")
        private Integer port;
        @Value("${spring.redis.password:}")
        private String password;
        @Value("${spring.redis.database:0}")
        private Integer database;
        @Value("${spring.redis.lettuce.pool.max-active:8}")
        private Integer maxActive;
        @Value("${spring.redis.lettuce.pool.max-idle:8}")
        private Integer maxIdle;
        @Value("${spring.redis.lettuce.pool.max-wait:-1}")
        private Long maxWait;
        @Value("${spring.redis.lettuce.pool.min-idle:0}")
        private Integer minIdle;
        @Bean
        public GenericObjectPoolConfig defaultPoolConfig() {
            GenericObjectPoolConfig config = new GenericObjectPoolConfig();
            config.setMaxTotal(maxActive);
            config.setMaxIdle(maxIdle);
            config.setMinIdle(minIdle);
            config.setMaxWaitMillis(maxWait);
            return config;
        }
        @Bean
        public RedisStandaloneConfiguration defaultRedisConfig() {
            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
            config.setHostName(host);
            config.setPassword(RedisPassword.of(password));
            config.setPort(port);
            config.setDatabase(database);
            return config;
        }
    }
}

基礎(chǔ)運(yùn)用

@Configuration注解最常見的搭配使用有兩個(gè):@Bean和@Scope

  • @Bean:等價(jià)于Spring中的bean標(biāo)簽用于注冊bean對象的,給容器中添加組件,一般以方法名作為組件的id,配置類里面使用@Bean標(biāo)注在方法上給容器注冊組件,默認(rèn)是單實(shí)例的。
@Configuration(proxyBeanMethods = true) //告訴springboot這是一個(gè)配置類 == 配置文件
public class Myconfig {
    @Bean//給容器中添加組件,以方法名作為組件的id。
    // 返回類型為組件類型,返回的值,就是組件在容器中的實(shí)例
    public User user01(){
        User wangcai = new User("wangcai",23);
        //user組件依賴了pet組件
        wangcai.setPet(pet01());
        return wangcai;
    }
    @Bean
    public Pet pet01(){
        return new Pet("旺財(cái)");
    }
}
  • @Scope:用于聲明該bean的作用域,作用域有singleton、prototype、request、session。
@Configuration
public class MyConfig {
    @Bean("user")
    @Scope(SCOPE_PROTOTYPE)         //多例
    public User getUser(){
        System.out.println("User對象進(jìn)行創(chuàng)建!");
        return new User("用戶", 22, getDog());
    }
    @Bean("dog")
    @Scope(SCOPE_SINGLETON)         //單例
    public Dog getDog(){
        System.out.println("Dog對象進(jìn)行創(chuàng)建!");
        return new Dog("金毛", 3);
    }
}

@Configuration注解的屬性

  • @Configuration注解中有@Component注解的加持,因此它自己本身也是一個(gè)bean對象,可以通過Context的進(jìn)行獲取。
  • @Configuration中的屬性proxyBeanMethods是及其重要的,設(shè)置true/false會(huì)得到不同的效果。
    • proxyBeanMethods = true的情況下,保持單實(shí)例對象
    • proxyBeanMethods = false的情況下,不進(jìn)行檢查IOC容器中是否存在,而是簡單的調(diào)用方法進(jìn)行創(chuàng)建對象,無法保持單實(shí)例
    • 簡單來說,就相當(dāng)于true只調(diào)用一次,而false會(huì)調(diào)用多次。

到此這篇關(guān)于SpringBoot中的@Configuration注解詳解的文章就介紹到這了,更多相關(guān)@Configuration注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • maven插件assembly使用及springboot啟動(dòng)腳本start.sh和停止腳本 stop.sh

    maven插件assembly使用及springboot啟動(dòng)腳本start.sh和停止腳本 stop.sh

    這篇文章主要介紹了maven插件assembly使用及springboot啟動(dòng)腳本start.sh和停止腳本 stop.sh的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 淺談Servlet 實(shí)現(xiàn)網(wǎng)頁重定向的方法

    淺談Servlet 實(shí)現(xiàn)網(wǎng)頁重定向的方法

    本篇文章主要介紹了Servlet 實(shí)現(xiàn)重定向幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java操作集合工具類Collections使用詳解

    Java操作集合工具類Collections使用詳解

    這篇文章主要介紹了java操作集合工具類Collections使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Kafka之kafka-topics.sh的使用解讀

    Kafka之kafka-topics.sh的使用解讀

    這篇文章主要介紹了Kafka之kafka-topics.sh的使用解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 使用Spring Boot Mybatis 搞反向工程的步驟

    使用Spring Boot Mybatis 搞反向工程的步驟

    這篇文章主要介紹了使用Spring Boot Mybatis 搞反向工程的步驟,幫助大家更好的理解和使用spring boot框架,感興趣的朋友可以了解下
    2021-01-01
  • MyBatis Plus工具快速入門使用教程

    MyBatis Plus工具快速入門使用教程

    這篇文章主要介紹了MyBatis Plus工具快速入門使用教程,需要的朋友可以參考下
    2018-05-05
  • java return用法實(shí)例詳解

    java return用法實(shí)例詳解

    在本篇文章里小編給大家整理的是關(guān)于java return用法以及相關(guān)知識點(diǎn)總結(jié),需要的朋友們參考下。
    2019-08-08
  • RocketMQ事務(wù)消息圖文示例講解

    RocketMQ事務(wù)消息圖文示例講解

    RocketMQ事務(wù)消息(Transactional Message)是指應(yīng)用本地事務(wù)和發(fā)送消息操作可以被定義到全局事務(wù)中,要么同時(shí)成功,要么同時(shí)失敗。RocketMQ的事務(wù)消息提供類似 X/Open XA 的分布式事務(wù)功能,通過事務(wù)消息能達(dá)到分布式事務(wù)的最終一致
    2022-12-12
  • 深入了解Java SpringBoot自動(dòng)裝配原理

    深入了解Java SpringBoot自動(dòng)裝配原理

    在使用springboot時(shí),很多配置我們都沒有做,都是springboot在幫我們完成,這很大一部分歸功于springboot自動(dòng)裝配。本文將詳細(xì)為大家講解SpringBoot的自動(dòng)裝配原理,需要的可以參考一下
    2022-03-03
  • 詳解Spring獲取配置的三種方式

    詳解Spring獲取配置的三種方式

    這篇文章主要為大家詳細(xì)介紹了Spring獲取配置的三種方式:@Value方式動(dòng)態(tài)獲取單個(gè)配置、@ConfigurationProperties+前綴方式批量獲取配置以及Environment動(dòng)態(tài)獲取單個(gè)配置,感興趣的可以了解一下
    2022-03-03

最新評論