@ConfigurationProperties(prefix = "person.dog")注解来修饰某类bean,其作用是告诉springBoot,获取配置文件中属性值,然后此类中的属性将与配置文件中对应属性一一绑定。【prefix = "person.dog"】表示与配置文件中哪个层级的属性进行绑定。如果是在主配置文件中(属性名必须是application.yml或application.properties)可以直接使用,实际生产中因为项目中一些属性为了直观区分,需自定义一个配置文件,例如redis.properties,这些属性在自定义的 配置文件中的,则需要配合@PropertySource使用.
默认Spring只读取主配置文件的属性,因此要引入自定义配置文件则要使用@PropertySource注解,并指定自定义配置文件路径,这样就可以将指定配置文件的属性读取到Spring中以供使用。我们可以通过environment.getProperty("env.name"),获取指定属性的值。也可以配合@Value 注解将Spring容器中读取到的值赋值给bean的属性。这种方式适合主要注入的属性少的场景,如果bean属性很多,那么要一个个的给属性标注@Value注解,过于繁琐,可以直接使用@ConfigurationProperties(prefix = "person.dog")自动适配属性。
person.properties配置文件内容
person.lastName=summer
person.age=18
person.address=南京
person.desc=${person.lastName} from ${person.address} and his age is ${person.age} and he has a cute dog named ${person.dog.name}
person.dog.name=花花
person.dog.age=8
env.name=summer_env
1.通过@PropertySource 引入配置文件 配合@Value 注解注入值
package com.summerSpringBoot.demo.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@PropertySource(value = { "classpath:person.properties" })
@Component
public class Person {
@Value("${person.lastName}")
private String name;
@Value("${person.age}")
private int age;
@Value("${person.address}")
private String address;
@Value("${person.desc}")
private String desc;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
2.直接通过@PropertySource 引入配置文件 配合@ConfigurationProperties(prefix = "person.dog") 自动赋值
package com.summerSpringBoot.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={ "classpath:person.properties" },encoding = "utf-8")
@ConfigurationProperties(prefix = "person.dog")
public class Dog {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.summerSpringBoot.demo.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.druid.stat.TableStat.Name;
import com.summerSpringBoot.demo.pojo.Dog;
import com.summerSpringBoot.demo.pojo.Order;
import com.summerSpringBoot.demo.pojo.Person;
import com.summerSpringBoot.demo.pojo.User;
@RestController
public class UserController {
@Value("${person.lastName}")
private String lastName;
@Autowired
private Person person;
@Autowired
private Dog dog;
@Autowired
private Environment environment;
@RequestMapping("/")
public Map index() {
Map<String, Object> map = new HashMap<>();
map.put("id", UUID.randomUUID().toString());
map.put("name", lastName);
map.put("person", person);
map.put("dog", dog);
String property = environment.getProperty("env.name");
map.put("env", property);
return map;
}
}
访问:http://127.0.0.1/summer/ 可以成功拿到值