100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > SpringBoot (六) 整合配置文件 @Value ConfigurationProperties

SpringBoot (六) 整合配置文件 @Value ConfigurationProperties

时间:2019-07-01 00:18:26

相关推荐

SpringBoot (六) 整合配置文件 @Value ConfigurationProperties

哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮

有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。

1 使用 @Value 注解

/** @Author : 有勇气的牛排* @FileName: ReadProperties.java* desc:* */package com.couragesteak.service;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ReadProperties {@Value("${cs.name}")private String name;@Value("${cs.star}")private String star;// http://127.0.0.1:8080/getProperties@RequestMapping("/getProperties")public String getProperties() {return name + ":" + star;}}

2 使用 ConfigurationProperties

@ConfigurationProperties加载配置通过 BeanPostProcessor实现,其对应的Bean的后置处理器为 ConfigurationPropertiesBindingPostProcessor,也就是在bean被实例化后,会调用后置处理器,递归的查找属性,通过反射机制注入值,因此需要提供getter和setter方法。

此外,针对此种属性注入的方式,SpringBoot支持Relaxed Bingding,即秩序保证配置文件的属性和setter方法名相同即可。

注意事项

(1) 属性必须要有getter、setter方法

(2) 如果属性类型是集合,需要确保集合是不可变的。

(3) 如果使用Lombok自动生成 getter/setter方法,一定不要生成对应的任何构造函数,因为Spring IOC容器会自动使用它来实例化对象。

(4) 使用JavaBean属性绑定的方式只针对标准Java Bean属性,不支持对静态属性的绑定。

配置文件

application.yml

cs:name: csstar: 9

2.1 Maven依赖

<!--导入配置文件处理器,配置文件进行绑定就会有提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>

2.2 实体类

/** @Author : 有勇气的牛排* @FileName: UserEntity.java* desc:* */package com.couragesteak.entity;import org.springframework.boot.context.properties.ConfigurationProperties;import org.ponent;@Component@ConfigurationProperties(prefix = "cs")public class UserEntity {private String name;private String star;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getstar() {return star;}public void setstar(String star) {this.star = star;}@Overridepublic String toString() {return "UserEntity{" +"name='" + name + '\'' +", star='" + star + '\'' +'}';}}

2.3 后端

/** @Author : 有勇气的牛排* @FileName: ReadProperties_ConfigurationProperties.java* desc: ConfigurationProperties 配置文件读取* */package com.couragesteak.service;import com.couragesteak.entity.UserEntity;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ReadProperties_ConfigurationProperties {@Autowiredprivate UserEntity userEntity;//@RequestMapping("/getInfo")public String getInfo(String userName, Integer age){System.out.println("666");return userEntity.toString();}}

注意事项

Spring考虑到带有注释@ConfigurationProperties的类可能不适合扫描(比如:我正在开发自己的自动配置或希望有条件地启用它们),所以Spring并不会自动扫描带有注释@ConfigurationProperties的类。

在这些情况下,推荐使用@EnableConfigurationProperties注解指定要处理的类型列表(即:将@ConfigurationProperties注释的类加到Spring IOC容器中)。一般通过将@EnableConfigurationProperties加在@Configuration类上完成。

3 @Value和@ConfigurationProperties的区别

(1) @ConfigurationProperties注解支持属性文件和javabean映射;而@Value支持spel表达式。

(2) @ConfigurationProperties注解支持全量的属性,宽松绑定方式;而@Vavlue只推荐使用标准的kebab-case方式(仅使用小写字母和-),例如@Value("{demo.item-price}")可以提取demo.item-pricedemo-itemPrice

(3) 对于多个属性映射,并且属性尝尝被复用时,推荐使用@ConfigurationProperties;只读取单个属性使用@Value更加方便。

参考地址:

余胜军/article/1058227/article/292

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。