最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java SpringBoot自動配置原理詳情

 更新時間:2022年07月27日 14:40:05   作者:WuSong1999  
這篇文章主要介紹了Java SpringBoot自動配置原理詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下

SpringBoot的底層注解

首先了解一些SpringBoot的底層注解,是如何完成相關(guān)的功能的

@Configuration 告訴SpringBoot被標(biāo)注的類是一個配置類,以前Spring xxx.xml能配置的內(nèi)容,它都可以做,spring中的Bean組件默認(rèn)是單實(shí)例的

#############################Configuration使用示例######################################################
/**
 * 1、配置類里面使用@Bean標(biāo)注在方法上給容器注冊組件,默認(rèn)也是單實(shí)例的
 * 2、配置類本身也是組件
 * 3、proxyBeanMethods:代理bean的方法
 *      Full(proxyBeanMethods = true)、【保證每個@Bean方法被調(diào)用多少次返回的組件都是單實(shí)例的】
 *      Lite(proxyBeanMethods = false)【每個@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的】
 *      組件依賴必須使用Full模式默認(rèn)。其他默認(rèn)是否Lite模式
 *
 *
 *
 */
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件
public class MyConfig {
    /**
     * Full:外部無論對配置類中的這個組件注冊方法調(diào)用多少次獲取的都是之前注冊容器中的單實(shí)例對象
     * @return
     */
    @Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        //user組件依賴了Pet組件
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}
################################@Configuration測試代碼如下########################################
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
public class MainApplication {
    public static void main(String[] args) {
        //1、返回我們IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
 
        //2、查看容器里面的組件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        //3、從容器中獲取組件
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("組件:"+(tom01 == tom02));

        //4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);
 
        //如果@Configuration(proxyBeanMethods = true)代理對象調(diào)用方法。SpringBoot總會檢查這個組件是否在容器中有。
        //保持組件單實(shí)例
        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println(user == user1);
        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);
        System.out.println("用戶的寵物:"+(user01.getPet() == tom));

    }
}

@Import注解詳解,將指定組件導(dǎo)入到容器中

 * 4、@Import({User.class, DBHelper.class})
 *      給容器中自動創(chuàng)建出這兩個類型的組件、默認(rèn)組件的名字就是全類名
 *
 *
 *
 */
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件
public class MyConfig {
}

@Conditional注解詳解,條件裝配,必須滿足Conditional指定的條件,才會繼續(xù)組件的注入

 以上是所有Conditional的實(shí)現(xiàn)注解

@ConditionalOnBean(name = "tom") 容器中有tom組件,才會進(jìn)行組件的注入

=====================測試條件裝配==========================
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
public class MyConfig {
    /**
     * Full:外部無論對配置類中的這個組件注冊方法調(diào)用多少次獲取的都是之前注冊容器中的單實(shí)例對象
     * @return
     */
    @Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例
    public User user01(){
        User zhangsan = new User("zhangsan", 18);
        //user組件依賴了Pet組件
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
 
    @Bean("tom22")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}
public static void main(String[] args) {
        //1、返回我們IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
 
        //2、查看容器里面的組件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        boolean tom = run.containsBean("tom");
        System.out.println("容器中Tom組件:"+tom);
        boolean user01 = run.containsBean("user01");
        System.out.println("容器中user01組件:"+user01);
        boolean tom22 = run.containsBean("tom22");
        System.out.println("容器中tom22組件:"+tom22);
    }

@ImportResource導(dǎo)入資源注解,一些老的項目還是會有xml配置的文件,該注解用于將這些xml文件導(dǎo)入進(jìn)來

======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 
    <bean id="haha" class="com.atguigu.boot.bean.User">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
    </bean>
 
    <bean id="hehe" class="com.atguigu.boot.bean.Pet">
        <property name="name" value="tomcat"></property>
    </bean>
</beans>
@ImportResource("classpath:beans.xml")
public class MyConfig {}
 
======================測試=================
        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha:"+haha);//true
        System.out.println("hehe:"+hehe);//true

配置綁定

使用java讀取到properties文件中的內(nèi)容,并且把它封裝到j(luò)avaBean中,以供隨時使用

public class getProperties {
     public static void main(String[] args) throws FileNotFoundException, IOException {
         Properties pps = new Properties();
         pps.load(new FileInputStream("a.properties"));
         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
         while(enum1.hasMoreElements()) {
             String strKey = (String) enum1.nextElement();
             String strValue = pps.getProperty(strKey);
             System.out.println(strKey + "=" + strValue);
             //封裝到JavaBean。
         }
     }
 }

@ConfigurationProperties 將properties里的內(nèi)容綁定到對應(yīng)的屬性當(dāng)中

只有在容器中的組件,才會擁有SpringBoot提供的強(qiáng)大的功能

/**
 * 只有在容器中的組件,才會擁有SpringBoot提供的強(qiáng)大功能
 */
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;
 
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

@EnableConfigurationProperties(Car.class)(開啟car配置綁定功能,把這個car這個組件自動注冊到容器中)

@ConfigurationProperties

自動配置原理入門

核心注解@SpringBootApplication相當(dāng)于三個注解,@SpringBootConfiguration、
@EnableAutoConfiguration、@ComponentScan

  • @SpringBootConfiguration:@Configuration。代表當(dāng)前是一個配置類
  • @ComponentScan:指定掃描范圍
  • @EnableAutoConfiguration:核心注解,它也是一個核心注解,它里面包括@AutoConfigurationPackage和
  • @Import(AutoConfigurationImportSelector.class)
  • @AutoConfigurationPackage:內(nèi)部是一個Import注解,就是給容器中導(dǎo)入組件
@Import(AutoConfigurationPackages.Registrar.class) ?//給容器中導(dǎo)入一個組件
public @interface AutoConfigurationPackage {}

//利用Registrar給容器中導(dǎo)入一系列組件
//將指定的一個包下的所有組件導(dǎo)入進(jìn)來,MainApplication 所在包下。

@Import(AutoConfigurationImportSelector.class)

  • 1、利用getAutoConfigurationEntry(annotationMetadata);給容器中批量導(dǎo)入一些組件
  • 2、調(diào)用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)獲取到所有需要導(dǎo)入到容器中的配置類
  • 3、利用工廠加載 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的組件
  • 4、從META-INF/spring.factories位置來加載一個文件。

默認(rèn)掃描我們當(dāng)前系統(tǒng)里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories

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

相關(guān)文章

  • 詳解Java面向?qū)ο笾鄳B(tài)的原理與實(shí)現(xiàn)

    詳解Java面向?qū)ο笾鄳B(tài)的原理與實(shí)現(xiàn)

    多態(tài)是指不同的子類在繼承父類后分別都重寫覆蓋了父類的方法,即父類同一個方法,在繼承的子類中表現(xiàn)出不同的形式。本文將詳解多態(tài)的原理與實(shí)現(xiàn),感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • Java常用正則表達(dá)式驗(yàn)證工具類RegexUtils.java

    Java常用正則表達(dá)式驗(yàn)證工具類RegexUtils.java

    相信大家對正則表達(dá)式一定都有所了解和研究,這篇文章主要為大家分享了Java 表單注冊常用正則表達(dá)式驗(yàn)證工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Feign?請求動態(tài)URL方式

    Feign?請求動態(tài)URL方式

    這篇文章主要介紹了Feign?請求動態(tài)URL方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java日志打印的完全使用指南

    java日志打印的完全使用指南

    日志就是記錄程序的運(yùn)行軌跡,方便查找關(guān)鍵信息,也方便快速定位解決問題,下面這篇文章主要給大家介紹了關(guān)于java日志打印使用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法

    Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法

    這篇文章主要介紹了Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法,需要的朋友可以參考下
    2018-04-04
  • Spring IOC源碼之bean的注冊過程講解

    Spring IOC源碼之bean的注冊過程講解

    這篇文章主要介紹了Spring IOC源碼之bean的注冊過程講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • spring?boot自動裝配之@ComponentScan注解用法詳解

    spring?boot自動裝配之@ComponentScan注解用法詳解

    @ComponentScan的作用就是根據(jù)定義的掃描路徑,把符合掃描規(guī)則的類裝配到spring容器中,下面這篇文章主要給大家介紹了關(guān)于spring?boot自動裝配之@ComponentScan注解用法的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Java中的復(fù)合數(shù)據(jù)類型

    Java中的復(fù)合數(shù)據(jù)類型

    這篇文章主要介紹了Java中的復(fù)合數(shù)據(jù)類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringMvc @Valid如何拋出攔截異常

    SpringMvc @Valid如何拋出攔截異常

    這篇文章主要介紹了SpringMvc @Valid如何拋出攔截異常,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • java插入排序 Insert sort實(shí)例

    java插入排序 Insert sort實(shí)例

    java插入排序 Insert sort實(shí)例代碼,需要的朋友可以參考一下
    2013-03-03

最新評論

托克逊县| 房山区| 临夏市| 夹江县| 枣强县| 察哈| 留坝县| 中超| 车险| 郓城县| 昌乐县| 长垣县| 徐州市| 怀远县| 家居| 临桂县| 德庆县| 宁德市| 荣成市| 静海县| 定日县| 惠州市| 房山区| 嘉鱼县| 镇宁| 文昌市| 交城县| 新宁县| 西乌| 乐平市| 昌吉市| 福清市| 务川| 怀宁县| 莱西市| 黄大仙区| 来宾市| 宜丰县| 彭山县| 任丘市| 淄博市|