學(xué)習(xí)SpringBoot容器功能及注解原理
1.組件添加
1.1@Configuration
@Configuration:告訴SpringBoot這是一個配置類
配置類里面使用@Bean標(biāo)注在方法上給容器注冊組件,默認(rèn)也是單實例的
配置類本身也是組件
proxyBeanMethods:代理bean的方法
- Full(
proxyBeanMethods = true):保證每個@Bean方法被調(diào)用多少次返回的組件都是單實例的 - Lite(
proxyBeanMethods = false):每個@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的 - 組件依賴必須使用Full模式默認(rèn)。其他默認(rèn)是否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)建出這兩個類型的組件、默認(rèn)組件的名字就是全類名
@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指定的條件,則進(jìn)行組件注入
有一系列派生注解:

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容器功能及注解原理的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot容器功能及注解的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用Maven插件生成Mybatis代碼的實現(xiàn)方法
本文主要介紹 Maven插件生成Mybatis代碼,現(xiàn)在做開發(fā)的朋友有好多用Maven 來管理代碼,這里給大家舉個例子,有需要的同學(xué)可以看下2016-07-07
Maven及Springboot配置JDK版本,編碼,源碼打包等方式
這篇文章主要介紹了Maven及Springboot配置JDK版本,編碼,源碼打包等方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java導(dǎo)出Excel統(tǒng)計報表合并單元格的方法詳解
我們在日常編程過程中,總是會碰見導(dǎo)出相關(guān)表格信息的需求,所以就讓我們一起來學(xué)習(xí)一下,這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Excel統(tǒng)計報表合并單元格的相關(guān)資料,需要的朋友可以參考下2021-10-10
Spring Boot集成kubernetes客戶端實現(xiàn)API操作k8s集群的方案
Kubernetes是一個開源的容器編排平臺,可以自動化在部署、管理和擴展容器化應(yīng)用過程中涉及的許多手動操作,這篇文章主要介紹了Spring Boot集成kubernetes客戶端實現(xiàn)API操作k8s集群,需要的朋友可以參考下2024-08-08
使用Java的Graphics類進(jìn)行繪圖的方法詳解
這篇文章主要介紹了使用Java的Graphics類進(jìn)行繪圖的方法,是Java的GUI編程的基礎(chǔ),需要的朋友可以參考下2015-10-10
Mybatis-Plus條件構(gòu)造器select方法返回指定字段方式
這篇文章主要介紹了Mybatis-Plus條件構(gòu)造器select方法返回指定字段方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
詳解基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢
這篇文章主要介紹了Java基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

