06-SpringBoot配置繫結
阿新 • • 發佈:2021-06-17
1.5 註解@ConfigurationProperties進行元件屬性和資源配置檔案中的值進行繫結
-
註解@ConfigurationProperties+@Component的方式將application.properties檔案中的值繫結到某個元件的屬性上。注意現在預設只能是繫結核心資源配置檔案中的標籤的值
1、在專案的bean包中新建一個Car類,其中有兩個屬性:String brand和Integer price
2、在application.properties核心資源配置檔案中自定義兩個標籤,並賦上值,如下
#自定義標籤:字首.字尾=值 #字首:表示的是這一類標籤,可以是有具體意義的,如這是要繫結到Car元件例項中的屬性,可以把字首 #定義為元件類名的首字母小寫,當然也可以是多級的字首xxx.zzz.yyy=ooo,主要用於過多使用該標籤 #的元件時多層次區分 #字尾:就是要把標籤的值繫結到元件中的屬性名 mycar.brand=Ford mycar.price=10253
3、在Car類中添加註解。如下
/** * @ConfigurationProperties: * 表示將這個類的屬性和核心資源配置檔案中的標籤值繫結 * 或者說就是給這個類的屬性進行賦值、注入 * 屬性: * prefix:表示字首,填的是檔案中標籤的字首 * 標籤中的字尾不用操作,它會自動匹配賦值 * 這個註解的屬性資源配置繫結只能和容器中的元件進行繫結,所以要將該類進行元件 * 註冊,這裡使用@Component */ @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 + '}'; } }
4、在主程式類中進行屬性值檢視,繫結成功
@SpringBootApplication public class BSpringbootAnnotationApplication { public static void main(String[] args) { //獲取容器物件 ConfigurableApplicationContext run = SpringApplication.run(BSpringbootAnnotationApplication.class, args); //測試第一種資源配置繫結 System.out.println("元件car的屬性:"+run.getBean("car")); //元件car的屬性:Car{brand='Ford', price=10253} } }
-
註解@ConfigurationProperties+@EnableConfigurationProperties的方式將application.properties檔案中的值繫結到某個元件的屬性上。注意現在預設只能是繫結核心資源配置檔案中的標籤的值。這種方式解決的是你可能引入的是第三方的類,當需要繫結時,這個類不一定有@Component,但你又不能在原始類上新增的情況
1、Car類中去掉@Component註解
2、在配置類的類名上新增@EnableConfigurationProperties,其中一個屬性是class型別的,需要我們填與繫結相關的類的Class物件,具體如下
/** * @EnableConfigurationProperties:翻譯過來就是可進行屬性資源繫結 * 作用: * 1、開啟指定Class的配置繫結功能,這裡是Car * 2、同時將Car這個類的物件作為元件放進容器中 */ @Configuration @EnableConfigurationProperties({Car.class})//第三方Car元件要進行繫結,把其放進容器中 public class MyConfig2 { /** * @ConditionalOnBean:條件裝配註解 * 裝配條件是容器中必須有該註解指定的元件 * (可以只用String集合name指定,也可以聯合Class集合value指定,或者String集合的type來指定)才會有裝配的效果 */ @ConditionalOnBean(name = "tomcat2",value = Pet.class) @Bean public User user02(){ User user = new User("李四"); return user; } //@Bean 讓容器中不建立tomcat2的Pet元件 public Pet tomcat2(){ Pet pet = new Pet("tomcat2"); return pet; } }
然後在主程式類中測試,也實現了配置繫結,但是@EnableConfigurationProperties幫我們註冊的Car例項元件在容器中的id是這樣的,如下:
@SpringBootApplication public class BSpringbootAnnotationApplication { public static void main(String[] args) { //獲取容器物件 ConfigurableApplicationContext run = SpringApplication.run(BSpringbootAnnotationApplication.class, args); //測試第一種資源配置繫結 //System.out.println("元件car的屬性:"+run.getBean("car"));//元件car的屬性:Car{brand='Ford', price=10253} //測試第二種資源配置繫結 String[] car = run.getBeanNamesForType(Car.class); System.out.println("@EnableConfigurationProperties幫我們建立的Car元件id:"+car[0]); //mycar-com.studymyself.bean.Car System.out.println("元件car的屬性:"+run.getBean("mycar-com.studymyself.bean.Car")); //元件car的屬性:Car{brand='Ford', price=10253} } } 結果: @EnableConfigurationProperties幫我們建立的Car元件id:mycar-com.studymyself.bean.Car 元件car的屬性:Car{brand='Ford', price=10253}