詳解如何實現(xiàn)SpringBoot的底層注解
一、@Configuration注解
1、基本使用
自定義配置類
/** * 1、@Configuration 告訴SpringBoot這是一個配置類,相當(dāng)于一個xml配置文件 * * 2、配置類里面使用 @Bean 標(biāo)注在方法上 來給容器注冊組件,默認(rèn)是單實例的 * * 3、配置類本身也是一個組件 */ @Configuration(proxyBeanMethods = true) public class MyConfig { @Bean public User user01(){ return new User("zhangsan",23); } @Bean public Pet pet01(){ return new Pet("cat"); } }
主程序類
/** * 主程序類 * @SpringBootApplication:這是一個SpringBoot應(yīng)用 */ @SpringBootApplication public class MainApplication { public static void main(String[] args) { //返回IOC容器 ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args); //從容器中獲取bean User user = context.getBean(User.class); System.out.println(user); Pet pet = context.getBean("pet01", Pet.class); System.out.println(pet); MyConfig myConfig = context.getBean(MyConfig.class); System.out.println(myConfig); /*如果配置@Configuration(proxyBeanMethods = true),代理對象調(diào)用方法從容器中拿組件,SpringBoot總會檢查容器中是否有這個組件 * 保持組件的單實例*/ User user1 = myConfig.user01(); User user2 = myConfig.user01(); System.out.println(user1 == user2); } }
打印結(jié)果:
2、Full模式與Lite模式
Full
模式是指proxyBeanMethods = true
,開啟代理bean的方法。此時調(diào)用配置類中每一個給而容器注冊組件方法,都會從容器中找組件,保持單例模式。
/*如果配置@Configuration(proxyBeanMethods = true),代理對象調(diào)用方法從容器中拿組件,SpringBoot總會檢查容器中是否有這個組件 * 保持組件的單實例*/ User user1 = myConfig.user01(); User user2 = myConfig.user01(); System.out.println(user1 == user2); //true
Lite
模式是指proxyBeanMethods = false
,關(guān)閉代理bean的方法。容器中不會保存代理對象,每一次調(diào)用配置類里面的方法,·都會產(chǎn)生一個新的對象。這可以解決組件依賴的問題。
User
組件里面有Pet
組件
public class User { private String name; private Integer age; private Pet pet; public Pet getPet() { return pet; } }
自定義的配置類中proxyBeanMethods
設(shè)置為false
@Configuration(proxyBeanMethods = false) public class MyConfig { @Bean public User user01(){ User user = new User("zhangsan", 23); //User組件依賴了Pet組件 user.setPet(pet01()); return user; } @Bean public Pet pet01(){ return new Pet("cat"); } }
主程序類
@SpringBootApplication public class MainApplication { public static void main(String[] args) { User user01 = context.getBean("user01", User.class); Pet pet01 = context.getBean("pet01", Pet.class); System.out.println(user01.getPet() == pet01); //true } }
打印結(jié)果:
- 配置類組件之間無依賴關(guān)系用
Lite
模式加速容器啟動過程,減少判斷 - 配置類組件之間有依賴關(guān)系,方法會被調(diào)用得到之前單實例組件,這時要用
Full
模式
二、@Import注解導(dǎo)入組件
@Import
注解可以給容器中自動創(chuàng)建出指定類型的組件,默認(rèn)組件的名字就是全類名
@Import(DBHelper.class) @Configuration(proxyBeanMethods = false) public class MyConfig { }
在容器中或者這個導(dǎo)入的組件
三、@Conditional注解條件裝配
滿足Conditional
指定的條件的方法,則進(jìn)行組件注入
也可以標(biāo)注在類上,當(dāng)容器中存在指定的組件的時候,配置類中的方法才會生效
四、@ImportResource注解導(dǎo)入Spring配置文件
外部配置文件
導(dǎo)入外部配置文件
獲取導(dǎo)入的外部組件
五、@ConfigurationProperties注解配置綁定
JavaBean與配置文件中屬性進(jìn)行綁定
需要使用@ConfigurationProperties
和 @Component
兩個注解,@Component
注解將組件注冊到容器中,因為只有在容器中的組件,才能使用SpringBoot提供的一些強(qiáng)大的注解功能
訪問請求獲取綁定的JavaBean
還可以在配置類中使用@EnableConfigurationProperties
開啟組件屬性配置功能,并把這個組件自動注冊到容器中,這樣就不需要再使用 @Component
注解
@EnableConfigurationProperties(Pet.class) public class MyConfig { }
//@Component @ConfigurationProperties(prefix = "mydog") public class Pet { }
依然可以訪問請求獲取綁定的JavaBean
到此這篇關(guān)于詳解如何實現(xiàn)SpringBoot的底層注解的文章就介紹到這了,更多相關(guān)SpringBoot底層注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析
這篇文章主要介紹了取消idea雙擊shift鍵時出現(xiàn)的全局搜索的問題分析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10JAVA把結(jié)果保留兩位小數(shù)的3種方法舉例
在寫程序的時候,有時候可能需要設(shè)置小數(shù)的位數(shù),所以下面這篇文章主要給大家介紹了關(guān)于JAVA把結(jié)果保留兩位小數(shù)的3種方法,文章通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08