亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1.組件添加

1.1@Configuration

@Configuration:告訴SpringBoot這是一個配置類

配置類里面使用@Bean標注在方法上給容器注冊組件,默認也是單實例的

配置類本身也是組件

proxyBeanMethods:代理bean的方法

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

最佳實戰(zhàn):

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

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

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

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

    @Bean//給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例
    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)代理對象調(diào)用方法。SpringBoot總會檢查這個組件是否在容器中有。
        //保持組件單實例
        User user = bean.user01();
        User user1 = bean.user01();
        //(proxyBeanMethods = true)返回true
        //(proxyBeanMethods = false)返回false
        System.out.println(user == user1);
    }

}

如果有組件依賴:

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

    @Bean//給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例
    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)入組件

代碼演示:

//給容器中自動無參構(gòu)造創(chuàng)建出這兩個類型的組件、默認組件的名字就是全類名
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件
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指定的條件,則進行組件注入

有一系列派生注解:

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這是一個配置類=配置文件
@ImportResource("classpath:beans.xml")
public class MyConfig {
}

測試:

@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中,以供隨時使用;

原生方法(配置文件復(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)建一個car類:

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

測試方法:

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

測試結(jié)果:

在這里插入圖片描述

3.2@EnableConfigurationProperties + @ConfigurationProperties

@EnableConfigurationProperties必須在配置類里寫:

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

該寫法就不用在寫@Component

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

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

相關(guān)文章

  • Mybatis多參數(shù)及實體對象傳遞實例講解

    Mybatis多參數(shù)及實體對象傳遞實例講解

    在使用Mybatis的時候,經(jīng)常會有各種各樣的參數(shù)傳遞,不同類型,不同個數(shù)的參數(shù),下面小編通過例子給大家講解下Mybatis多參數(shù)及實體對象傳遞,一起看看吧
    2016-12-12
  • idea同時編輯多行問題-win&mac都支持

    idea同時編輯多行問題-win&mac都支持

    這篇文章主要介紹了idea同時編輯多行問題-win&mac都支持,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • springBoot整合jwt實現(xiàn)token令牌認證的示例代碼

    springBoot整合jwt實現(xiàn)token令牌認證的示例代碼

    實施Token驗證的方法挺多的,還有一些標準方法,比如JWT,本文主要介紹了springBoot整合jwt實現(xiàn)token令牌認證的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • SpringBoot整合EasyExcel實現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)

    SpringBoot整合EasyExcel實現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)

    這篇文章主要為大家詳細介紹了如何使用Vue、SpringBoot和EasyExcel實現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • Java設(shè)計模式之命令模式詳細解析

    Java設(shè)計模式之命令模式詳細解析

    這篇文章主要介紹了Java設(shè)計模式之命令模式詳細解析,命令模式將請求封裝成對象,以便使用不同的請求、隊列或者日志來參數(shù)化其他對象,同時也支持可撤銷的操作,需要的朋友可以參考下
    2024-01-01
  • SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir解決方案

    SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempt

    這篇文章主要介紹了SVN出現(xiàn)提示org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir解決方案的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • Spring的FactoryBean<Object>接口示例代碼

    Spring的FactoryBean<Object>接口示例代碼

    FactoryBean是Spring框架中的一個接口,用于創(chuàng)建和管理Bean對象,它的作用是將Bean的創(chuàng)建過程交給FactoryBean實現(xiàn)類來完成,而不是直接由Spring容器來創(chuàng)建,本文給大家介紹Spring的FactoryBean<Object>接口,感興趣的朋友一起看看吧
    2023-11-11
  • Springboot?手動分頁查詢分批批量插入數(shù)據(jù)的實現(xiàn)流程

    Springboot?手動分頁查詢分批批量插入數(shù)據(jù)的實現(xiàn)流程

    這篇文章主要介紹了Springboot?手動分頁查詢分批批量插入數(shù)據(jù)的實現(xiàn)流程,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • SpringBoot項目@Async方法問題解決方案

    SpringBoot項目@Async方法問題解決方案

    這篇文章主要介紹了SpringBoot項目@Async方法問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Mybatis-Plus處理Mysql?Json類型字段的詳細教程

    Mybatis-Plus處理Mysql?Json類型字段的詳細教程

    這篇文章主要給大家介紹了關(guān)于Mybatis-Plus處理Mysql?Json類型字段的詳細教程,Mybatis-Plus可以很方便地處理JSON字段,在實體類中可以使用@JSONField注解來標記JSON字段,同時在mapper.xml中使用json函數(shù)來操作JSON字段,需要的朋友可以參考下
    2024-01-01

最新評論