當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot底层注解-@ConfigurationProperties配置绑定
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot底层注解-@ConfigurationProperties配置绑定
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
如何使用Java讀取到properties文件中的內(nèi)容,并且把它封裝到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。}}}@ConfigurationProperties
/*** 只有在容器中的組件,才會擁有SpringBoot提供的強(qiáng)大功能*/ @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;}@Overridepublic String toString() {return "Car{" +"brand='" + brand + '\'' +", price=" + price +'}';} }@EnableConfigurationProperties + @ConfigurationProperties
@Component + @ConfigurationProperties
@EnableConfigurationProperties(Car.class) //1、開啟Car配置綁定功能 //2、把這個Car這個組件自動注冊到容器中 public class MyConfig { }總結(jié)
以上是生活随笔為你收集整理的SpringBoot底层注解-@ConfigurationProperties配置绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot底层注解-@Impo
- 下一篇: SpringBoot自动配置【源码分析】