springboot配置文件注入——@ConfigurationProperties獲取值和@Value獲取值比較

@ConfigurationProperties獲取值和@Value獲取值比較 https://blog.csdn.net/clmmei_123/article/details/81871836

spring boot 使用@ConfigurationProperties https://blog.csdn.net/yingxiake/article/details/51263071

JSR 303 - Bean Validation 介紹及最佳實踐 https://www.ibm.com/developerworks/cn/java/j-lo-jsr303/index.html

使用JSR-303進行後臺數據校驗 https://www.cnblogs.com/baizhanshi/p/8109072.html

四、@PropertySource&@ImportResource&@Bean詳解。以及配置文件佔位符 https://www.jianshu.com/p/d2e29ed2004c

配置文件

person: 
     lastName: hello 
     age: 18 
     boss: false 
     birth: 2017/12/12 
     maps: {k1: v1,k2: 12} 
     lists:
          ‐ lisi 
          ‐ zhaoliu 
     dog:
         name: 小狗 
         age: 12

javaBean:

/*** 將配置文件中配置的每一個屬性的值,映射到這個組件中 
* @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定;
*  
* prefix = "person":配置文件中哪個下面的所有屬性進行一一映射 
* 
*  只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; */ 
@Component 
@ConfigurationProperties(prefix = "person") 
public class Person { 
     private String lastName; 
     private Integer age; 
     private Boolean boss; 
     private Date birth; 
     private Map<String,Object> maps; 
     private List<Object> lists; 
     private Dog dog;

我們可以導入配置文件處理器,以後編寫配置就有提示了

<!‐‐導入配置文件處理器,配置文件進行綁定就會有提示‐‐> 
     <dependency> 
          <groupId>org.springframework.boot</groupId> 
          <artifactId>spring‐boot‐configuration‐processor</artifactId>       
          <optional>true</optional> 
     </dependency>

1、properties配置文件在idea中默認utf-8可能會亂碼
調整

在這裏插入圖片描述
2、@Value獲取值和@ConfigurationProperties獲取值比較
在這裏插入圖片描述
配置文件yml還是properties他們都能獲取到值;

如果說,我們只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用@Value;

如果說,我們專門編寫了一個javaBean來和配置文件進行映射,我們就直接使用@ConfigurationProperties;

3、配置文件注入值數據校驗(person.java文件)

@Component 
@ConfigurationProperties(prefix = "person") 
@Validated 
public class Person {
 /*** <bean class="Person"> 
 * <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/# {SpEL}"></property>
 * <bean/> 
 */ 
 
 //lastName必須是郵箱格式 
 @Email 
 //@Value("${person.last‐name}") 
 private String lastName;
  //@Value("#{11*2}")
 private Integer age; 
 //@Value("true") 
 private Boolean boss; 
 
 private Date birth; 
 private Map<String,Object> maps;
 private List<Object> lists; 
 private Dog dog;

4、@PropertySource&@ImportResource&@Bean @PropertySource:加載指定的配置文件;

/*** 將配置文件中配置的每一個屬性的值,映射到這個組件中 
* @ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定; 
* prefix = "person":配置文件中哪個下面的所有屬性進行一一映射 *
* 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;
* @ConfigurationProperties(prefix = "person")默認從全局配置文件中獲取值;
**/ 
@PropertySource(value = {"classpath:person.properties"}) 
@Component 
@ConfigurationProperties(prefix = "person") 
//@Validated
public class Person {
 /*** <bean class="Person"> 
 * <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/# {SpEL}"></property>
 *  * <bean/> 
 * */ 
 * //lastName必須是郵箱格式 
 * // @Email 
 * //@Value("${person.last‐name}")
 private String lastName;
 //@Value("#{11*2}") 
private Integer age; 
//@Value("true") 
private Boolean boss;

@ImportResource:導入Spring的配置文件,讓配置文件裏面的內容生效; Spring Boot裏面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別; 想讓Spring的配置文件生效,加載進來;@ImportResource標註在一個配置類上

@ImportResource(locations = {"classpath:beans.xml"}) 
導入Spring的配置文件讓其生效

不來編寫Spring的配置文件

<?xml version="1.0" encoding="UTF‐8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring‐beans.xsd"> 
     <bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean> 
 </beans>

SpringBoot推薦給容器中添加組件的方式;推薦使用全註解的方式
1、配置類@Configuration------>Spring配置文件
2、使用@Bean給容器中添加組件

/**
* @Configuration:指明當前類是一個配置類;就是來替代之前的Spring配置文件 
* 在配置文件中用<bean><bean/>標籤添加組件 
*/
@Configuration 
public class MyAppConfig { 
    //將方法的返回值添加到容器中;容器中這個組件默認的id就是方法名 
    @Bean 
    public HelloService helloService02(){ 
    System.out.println("配置類@Bean給容器中添加組件了..."); 
    return new HelloService(); 
    } 
}

4、配置文件佔位符

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章