SpringBoot从配置⽂件中获取属性的四种⽅法
SpringBoot从配置⽂件中获取属性
⽅式⼀: @Value
基本类型属性注⼊,直接在字段上添加@Value("${}")即可.注意这⾥⽤的是$,⽽不是#.@Value注⼊的属性,⼀般其他属性没有关联关系.
配置⽂件
user:properties是什么文件
name: Manaphy
age:19
sex: male
@RestController
public class ConfigPropertiesController {
@Value("${user.name}")
private String name;
@Value("${user.age}")
private Integer age;
@Value("${user.sex}")
private String sex;
@GetMapping("/user")
public String getUser(){
return"{name:"+ name +",age:"+ age +",sex:"+ sex +"}";
}
}
⽅式⼆: @ConfigurationProperties
配置⽂件
person:
lastName: hello
age:18
boss:false
birth: 2017/12/12
maps:{k1: v1,k2: v2}
lists:
- lisi
- wangwu
dog:
name:⼩狗
age:12
JavaBean
/**
* 将配置⽂件中配置的每⼀个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置⽂件中相关的配置进⾏绑定; * prefix = "person":配置⽂件中哪个下⾯的所有属性进⾏⼀⼀映射
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能
*/
@Component
@ConfigurationProperties(prefix ="person")
@Data
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;
}
@Data
class Dog {
private String name;
private Integer age;
}
Controller层
@RestController
public class PersonController {
php编程画出以下图形@Autowired
private Person person;
@GetMapping("/person")
public Person getPerson(){
return person;
}
}
运⾏结果如下
我们可以导⼊配置⽂件处理器,以后编写配置就有提⽰了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@Value和@ConfigurationProperties⽐较
@ConfigurationProperties@Value 功能批量注⼊配置⽂件中的属性⼀个个指定松散绑定(松散语法)⽀持不⽀持SpEL不⽀持⽀持JSR303数据校验⽀持不⽀持
复杂类型封装⽀持不⽀持配置⽂件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取⼀下配置⽂件中的某项值,使⽤@Value;
如果说,我们专门编写了⼀个javaBean来和配置⽂件进⾏映射,我们就直接使⽤@ConfigurationProperties;
⽅式三: @PropertySource
配置⽂件
person.lastName=chen
person.age=22
person.boss=false
person.birth=2017/12/12
person.map1.k1=v1
person.map1.k2=v2
person.map2[k1]=v1
person.map2[k2]=v2
person.list[0]=zhang
person.list[1]=wang
person.list2=zhao,qian,sun,li
person.dog.name=⼩狗
person.dog.age=12
JavaBean
@Component
@ConfigurationProperties(prefix ="person")
//如果只有⼀个主配置类⽂件,@PropertySource可以不写
@PropertySource("classpath:person.properties")
@Data
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> map1;
private Map<String, Object> map2;
private List<Object> list;
private List<Object> list2;
private Dog dog;
}
@Data
class Dog {
private String name;
private Integer age;
}
⽅式四: 使⽤⼯具类⽆需注⼊获取.yml中的值
1. 新建 BeanConfiguration 类,⽤于项⽬启动构造我们的⼯具类
@Configuration
@Slf4j
public class BeanConfiguration {
@Bean
public YamlConfigurerUtil ymlConfigurerUtil(){
//1:加载配置⽂件
Resource app =new ClassPathResource("l");
Resource appDev =new ClassPathResource("l");
Resource appProd =new ClassPathResource("l");
YamlPropertiesFactoryBean yamlPropertiesFactoryBean =new YamlPropertiesFactoryBean();
// 2:将加载的配置⽂件交给 YamlPropertiesFactoryBean
yamlPropertiesFactoryBean.setResources(app);
// 3:将yml转换成 key:valpython视频教程网站
Properties properties = Object();
String active = null;
if(properties != null){
active = Property("spring.profiles.active");
}
if(StringUtils.isEmpty(active)){
<("未到spring.profiles.active配置!");
}else{
//判断当前配置是什么环境
if("dev".equals(active)){
yamlPropertiesFactoryBean.setResources(app, appDev);
}else if("prod".equals(active)){
yamlPropertiesFactoryBean.setResources(app, appProd);
}
}
/
/ 4: 将Properties 通过构造⽅法交给我们写的⼯具类
return new Object());
}
}
2. ⼯具类实现
public class YamlConfigurerUtil {
private static Properties ymlProperties =new Properties();
public YamlConfigurerUtil(Properties properties){
ymlProperties = properties;
}
public static String getStrYmlVal(String key){
Property(key);
网页素材打包下载}
滑块滑槽public static Integer getIntegerYmlVal(String key){
return Integer.Property(key));
}
}
3. 调⽤⽰例
public class PersonController {
@GetMapping("/msg")
public String getMessage(){
StrYmlVal("person.lastName"); }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。