接上篇: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使用场景区别
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。