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

學(xué)習(xí)SpringBoot容器功能及注解原理

 更新時(shí)間:2021年09月23日 10:18:20   作者:LL.LEBRON  
這篇文章主要介紹了學(xué)習(xí)SpringBoot容器功能及注解原理,文中通過詳細(xì)的代碼示例對(duì)SpringBoot容器功能及注解原理進(jìn)行了解析,有需要的朋友可以借鑒參考下

1.組件添加

1.1@Configuration

@Configuration:告訴SpringBoot這是一個(gè)配置類

配置類里面使用@Bean標(biāo)注在方法上給容器注冊(cè)組件,默認(rèn)也是單實(shí)例的

配置類本身也是組件

proxyBeanMethods:代理bean的方法

  • Full(proxyBeanMethods = true):保證每個(gè)@Bean方法被調(diào)用多少次返回的組件都是單實(shí)例的
  • Lite(proxyBeanMethods = false):每個(gè)@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的
  • 組件依賴必須使用Full模式默認(rèn)。其他默認(rèn)是否Lite模式

最佳實(shí)戰(zhàn):

1.配置類組件之間無依賴關(guān)系用Lite模式加速容器啟動(dòng)過程,減少判斷

2.配置類組件之間有依賴關(guān)系,方法會(huì)被調(diào)用得到之前單實(shí)例組件,用Full模式

代碼實(shí)戰(zhàn)演示:

@Configuration(proxyBeanMethods = false)//告訴SpringBoot這是一個(gè)配置類=配置文件
public class MyConfig {

    @Bean//給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例
    public User user01() {
        User zhangsan = new User("zhangsan", 18);
        return zhangsan;
    }

    @Bean("tom")//也可以自己設(shè)置id代替方法名作為id
    public Pet tomcatPet() {
        return new Pet("tomcat");
    }

}
@SpringBootApplication
public class Boot01HelloworldApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);//com.atguigu.boot.config.MyConfig@d67d8
        //如果@Configuration(proxyBeanMethods = true)代理對(duì)象調(diào)用方法。SpringBoot總會(huì)檢查這個(gè)組件是否在容器中有。
        //保持組件單實(shí)例
        User user = bean.user01();
        User user1 = bean.user01();
        //(proxyBeanMethods = true)返回true
        //(proxyBeanMethods = false)返回false
        System.out.println(user == user1);
    }

}

如果有組件依賴:

@Configuration(proxyBeanMethods = true)//告訴SpringBoot這是一個(gè)配置類=配置文件
public class MyConfig {

    @Bean//給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實(shí)例
    public User user01() {
        User zhangsan = new User("zhangsan", 18);
        //user組件依賴了Pet組件
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
    @Bean("tom")//也可以自己設(shè)置id代替方法名作為id
    public Pet tomcatPet() {
        return new Pet("tomcat");
    }

}
@SpringBootApplication
public class Boot01HelloworldApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);
        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);
		//(proxyBeanMethods = true)返回(用戶的寵物:true)
        //(proxyBeanMethods = false)返回(用戶的寵物:false)
        System.out.println("用戶的寵物:"+(user01.getPet() == tom));
    }

}

1.2@Import

@Import:給容器中導(dǎo)入組件

代碼演示:

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

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
        //獲取組件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        DBHelper bean = run.getBean(DBHelper.class);
        System.out.println(bean);
    }
}

//輸出:
com.atguigu.boot.bean.User
ch.qos.logback.core.db.DBHelper@16ef799

1.3@Conditional

@Conditional:條件裝配,滿足Conditional指定的條件,則進(jìn)行組件注入

有一系列派生注解:

img

2.原生配置文件引入

2.1@ImportResource

原生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>

自定義配置類:

@Configuration(proxyBeanMethods = true)//告訴SpringBoot這是一個(gè)配置類=配置文件
@ImportResource("classpath:beans.xml")
public class MyConfig {
}

測(cè)試:

@SpringBootApplication
public class Boot01HelloworldApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);

        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println(haha);//true
        System.out.println(hehe);//true
    }
}

3.配置綁定

如何使用Java讀取到properties文件中的內(nèi)容,并且把它封裝到JavaBean中,以供隨時(shí)使用;

原生方法(配置文件復(fù)雜就顯得麻煩):

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。
         }
     }
 }

3.1@ConfigurationProperties

配置文件:

mycar.brand=BYD
mycar.price=100000

創(chuàng)建一個(gè)car類:

//只有在容器中的組件,才會(huì)擁有SpringBoot提供的強(qiáng)大功能
@Component
@ConfigurationProperties(prefix = "mycar")
//Lombok注解簡(jiǎn)化開發(fā)
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Car {
    private String brand;
    private Integer price;
}

測(cè)試方法:

@RestController
public class HelloController {
    @Autowired
    Car car;
    @RequestMapping("/car")
    public Car car(){
        return car;
    }
}

測(cè)試結(jié)果:

在這里插入圖片描述

3.2@EnableConfigurationProperties + @ConfigurationProperties

@EnableConfigurationProperties必須在配置類里寫:

@Configuration(proxyBeanMethods = true)//告訴SpringBoot這是一個(gè)配置類=配置文件
@EnableConfigurationProperties(Car.class)
//1.開啟Car屬性配置綁定功能
//2.把Car這個(gè)組件自動(dòng)注冊(cè)到容器中
public class MyConfig {
}

該寫法就不用在寫@Component

@ConfigurationProperties(prefix = "mycar")
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Car {
    private String brand;
    private Integer price;
}

以上就是學(xué)習(xí)SpringBoot容器功能及注解原理的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot容器功能及注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

蒲江县| 泗洪县| 邯郸市| 墨脱县| 万宁市| 巢湖市| 南和县| 津市市| 康保县| 桂东县| 广平县| 汝城县| 灵武市| 常宁市| 民乐县| 准格尔旗| 天峻县| 龙游县| 鹤庆县| 佛教| 新蔡县| 丹棱县| 芷江| 农安县| 和平县| 镇康县| 客服| 荆州市| 察雅县| 汉阴县| 团风县| 鲁山县| 利津县| 凌源市| 溧水县| 华亭县| 瓮安县| 钟山县| 郑州市| 巴彦淖尔市| 闽侯县|