SpringBoot2底層注解@ConfigurationProperties配置綁定
SpringBoot2底層注解@ConfigurationProperties的配置綁定
我們通常會把一些經常變動的東西放到配置文件里。
比如之前寫在配置文件application.properties里的端口號server.port=8080,另外常見的還有數據庫的連接信息等等。
那么,我的數據庫連接信息放在配置文件里,我要使用的話肯定得去解析配置文件,解析出的內容在 bean 里面去使用。
整個場景其實就是把配置文件里的所有配置,綁定到 java bean 里面。
要完成這個場景,基于 java 原生代碼編寫還是有點麻煩的。通常會做一個封裝,讀取到properties文件中的內容,并且把它封裝到JavaBean中:
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 ... ... } }
這里就是使用Properties類來加載配置文件a.properties,然后遍歷配置文件中的每一個k-v,獲取之后就可以用到對應的地方。
在 springboot 中簡化了這個過程,這就是配置綁定。
配置綁定
通過使用注解@ConfigurationProperties來完成配置綁定,注意需要結合@Component使用。
新建一個組件Car,有2個屬性分別是品牌和價格:
@Component public class Car { private String brand; private Integer price; // get set tostring 就不貼了
在配置文件application.properties,設置一些屬性值,比如:
mycar.brand=QQmycar.price=9999
使用@ConfigurationProperties注解,加到組件上:
mycar.brand=QQ mycar.price=9999
傳進去的 prefix 是配置文件里的前綴,這里就是 mycar。
驗證
現在來測試一下是否綁定成功,在之前的HelloController繼續(xù)增加一個控制器方法:
@RestController public class HelloController { @Autowired Car car; @RequestMapping("/car") public Car car() { return car; } @RequestMapping("/hello") public String Hello() { return "Hello SpringBoot2 你好"; } }
部署一下應用,瀏覽器訪問http://localhost:8080/car:
綁定成功。
另一種方式
除上述方法之外,還可以使用@EnableConfigurationProperties + @ConfigurationProperties的方式來完成綁定。
注意,@EnableConfigurationProperties注解要使用在配置類上,表示開啟屬性配置的功能:
//@ConditionalOnBean(name = "pet1") @Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = true) @ImportResource("classpath:beans.xml") //配置文件的類路徑 @EnableConfigurationProperties(Car.class) //開啟屬性配置的功能 public class MyConfig { @Bean("user1") public User user01(){ User pingguo = new User("pingguo",20); pingguo.setPet(tomcatPet()); return pingguo; } @Bean("pet22") public Pet tomcatPet(){ return new Pet("tomcat"); } }
@EnableConfigurationProperties(Car.class)傳入要開啟配置的類,這可以自動的把 Car 注冊到容器中,也就是說之前 Car 上的@Component就不需要了。
//@Component @ConfigurationProperties(prefix = "mycar") public class Car { private String brand; private Integer price;
重新部署訪問下地址,依然可以。
關于第二種的使用場景,比如這里的 Car 是一個第三方包里的類,但是人家源碼沒有加@Component注解,這時候就可以用這種方式進行綁定。
最后,要記住當使用@ConfigurationProperties(prefix = "mycar")這個配置綁定時,是跟 springboot 核心配置文件 application.properties文件的內容建立的綁定關系。
以上就是SpringBoot2底層注解@ConfigurationProperties配置綁定的詳細內容,更多關于SpringBoot2注解配置綁定的資料請關注腳本之家其它相關文章!
- SpringBoot中的@ConfigurationProperties注解解析
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- 關于SpringBoot的@ConfigurationProperties注解和松散綁定、數據校驗
- SpringBoot中@ConfigurationProperties注解實現配置綁定的三種方法
- SpringBoot中注解@ConfigurationProperties與@Value的區(qū)別與使用詳解
- SpringBoot @ConfigurationProperties注解的簡單使用
- Springboot之@ConfigurationProperties注解解讀
相關文章
java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解
這篇文章主要為大家詳細介紹了java中timer的schedule和scheduleAtFixedRate方法區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12SpringMvc+Mybatis+Pagehelper分頁詳解
這篇文章主要介紹了SpringMvc+Mybatis+Pagehelper分頁詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下的相關資料2017-01-01關于springboot加載yml配置文件的no字段自動轉義問題
這篇文章主要介紹了關于springboot加載yml配置文件的no字段自動轉義問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02