一、配置文件值注入
1、在src/main/resource下创建配置文件application.properties,内容如下:
person.lastName=hello___${lastName:wuyuning} //lastName不存在的时候,默认值是wuyuning
person.age=${random.int} //随机生成一个int范围的随机数
person.birth=2017/12/15
person.boss=false
person.maps.aa=beijing
person.maps.bb=shanghai
person.lists=a,b,c
person.dog.name=${hello:hello}___dog
person.dog.age=15
address=hangzhou
lastName=name***
password=def0000
备注:也可用application.yml的配置文件,如果两者同时存在,则yml的优先级高于properties
二、测试使用
1、指定bean对象
package com.wuyuning.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
import java.util.Map;
//如果不是默认application.properties默认配置文件,需要加下面这行,指定配置文件
//@PropertySource(value = {"classpath:person.properties"})
@Component//将Person类注入到容器中
//告诉SpingBoot将被本类中的所有属性和配置文件中相关的配置进行绑定;
//类中属性必须与配置文件一致才能注入进来
@ConfigurationProperties(prefix = "person")
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;
public Person(){
}
public String toSting(){
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public Person(String lastName, Integer age, Boolean boss, Date bir, Map<String, Object> maps, List<Object> lists, Dog dog) {
this.lastName = lastName;
this.age = age;
this.boss = boss;
this.birth = birth;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
2、测试。创建一个MyController类,内容如下:
package com.wuyuning.springboot.controller;
import com.wuyuning.springboot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired
Person person;
@Value("${address}")
String address;
//
// @Value("${user.lastName}")
// String lastName;
@RequestMapping("/yml1")//设置默认路径
@ResponseBody
public Person propertiesTest(){
return person;
}
@RequestMapping("/yml2")//设置默认路径
@ResponseBody
public String propertiesTest02(){
return address;
}
@Value("${password}")
String password;
@RequestMapping("/yml3")//设置默认路径
@ResponseBody
public String propertiesTes3(){
return "profile = "+password;
}
}
在网页中输入:
localhost:8080/yml1
localhost:8080/yml2
localhost:8080/yml3
Profifile环境切换:
我们在主配置文件编写的时候,文件名可以是 application-{profifile}.properties/yml 默认使用application.properties 的配置;例如:application-dev.properties :开发环境 application-test.properties :测试环境 applicationprod.properties :生产环境