网站页面模板 建设中,西安发布信息的平台,下载安装,网站是怎么建立起来的哈喽#xff0c;大家好#xff0c;我是有勇气的牛排#xff08;全网同名#xff09;#x1f42e;#x1f42e;#x1f42e;
有问题的小伙伴欢迎在文末评论#xff0c;点赞、收藏是对我最大的支持#xff01;#xff01;#xff01;。
1 使用 Value 注解
/** Auth…哈喽大家好我是有勇气的牛排全网同名
有问题的小伙伴欢迎在文末评论点赞、收藏是对我最大的支持。
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;RestController
public class ReadProperties {Value(${cs.name})private String name;Value(${cs.star})private String star;// http://127.0.0.1:8080/getPropertiesRequestMapping(/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: 92.1 Maven依赖
!--导入配置文件处理器配置文件进行绑定就会有提示--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional
/dependency2.2 实体类
/** Author : 有勇气的牛排* FileName: UserEntity.java* desc :* */package com.couragesteak.entity;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;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;RestController
public 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-price和demo-itemPrice。
(3) 对于多个属性映射并且属性尝尝被复用时推荐使用ConfigurationProperties只读取单个属性使用Value更加方便。
参考地址
余胜军https://developer.aliyun.com/article/1058227https://www.couragesteak.com/article/292