SpringBoot/Spring使用@Value進行屬性綁定(尚硅谷)


接上篇:springboot使用ConfigurationProperties注解讀取自定義屬性(傳智播客代碼)

部分代碼參考上篇,這里用@Value讀取值

1、@Value讀取數據

@Value支持字面量、Spring EL表達式(#),美元符號($),相比於@ConfigurationProperties,他用$取值時需要完全路徑

package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Person {

    /***
     * 美元表達式(從配置文件、系統環境變量讀取)
     */
    @Value("${person.lastName}")
    private String lastName;

    /***
     * Spring EL表達式
     */
    @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;
}

 

 

 

 2、ConfigurationProperties注解JSR303數據校驗

package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
@Validated public class Person {

    @Email private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

由於lastName不是有效的郵箱,啟動時即報錯

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'person' to com.atguigu.bean.Person failed:

    Property: person.lastName
    Value: zhangsan
    Origin: class path resource [application.properties]:2:17
    Reason: 不是一個合法的電子郵件地址

 

3、@Value不支持復合數據類型綁定

測試用例

package com.atguigu;

import com.atguigu.bean.Person;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class DemoApplicationTests {

    @Autowired
    Person person;

    @Test
    void customPropertiesTest()
    {
        log.info("{}",person);
    }
}

3.1、 $(美元符號讀取map直接報錯)

package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Person {

    /***
     * 美元表達式(從配置文件、系統環境變量讀取)
     */
    @Value("${person.lastName}")
    private String lastName;

    /***
     * Spring EL表達式
     */
    @Value("#{11*2}")
    private Integer age;

    /***
     * 字面量
     */ 
    @Value("true")
    private Boolean boss;
    private Date birth;

    @Value("${person.maps}")
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

 

 

3、2使用spring el表達式獲取不到值

package com.atguigu.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Person {

    /***
     * 美元表達式(從配置文件、系統環境變量讀取)
     */
    @Value("${person.lastName}")
    private String lastName;

    /***
     * Spring EL表達式
     */
    @Value("#{11*2}")
    private Integer age;

    /***
     * 字面量
     */ 
    @Value("true")
    private Boolean boss;
    private Date birth;

    @Value("#{person.maps}")
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

 

4、@ConfigurationProperties和@Value使用場景區別

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM