springboot配置
环境
springboot1.5.9
1.springboot加载配置顺序
1.1 springboot启动会扫描一下位置的application.properties或者application.yml作为默认的配置文件
工程根目录:./config/
工程根目录:./
classpath:/config/
classpath:/
加载的优先级顺序是从上向下加载,并且所有的文件都会被加载,高优先级的内容会覆盖底优先级的内容,形成互补配置
也可以通过指定配置spring.config.location来改变默认配置,一般在项目已经打包后,我们可以通过指令
java -jar xxxx.jar --spring.config.location=/**/application.yml来加载外部的配置
2.springboot加载自定义属性
三种方式,见下面示例
bean映射
@Component
//指定配置路径
@PropertySource("classpath:conf/user.properties")
@ConfigurationProperties(prefix="test")
public class UserInfo2 {
private String userName;
private String passWord;
......
}
使用
@RestController
public class TestController {
@Value("${com.ctg.test.userName}")
private String userName;
@Value("${com.ctg.test.passWord}")
private String passWord;
@Autowired
private Environment environment;
@Autowired
private UserInfo userInfo ;
@Autowired
private UserInfo2 userInfo2 ;
@RequestMapping(value = "/test")
@ResponseBody
public Object test(){
Map<String,Object>map =new HashMap<>();
map.put("code",200);
map.put("@Value读取:",userName+":"+passWord);
map.put("environment读取:",environment.getProperty("com.ctg.test.userName")+":"+environment.getProperty("com.ctg.test.passWord"));
map.put("Bean读取:",userInfo.getUserName()+":"+userInfo.getPassWord());
map.put("Bean读取指定配置路径:",userInfo2.getUserName()+":"+userInfo2.getPassWord());
return map;
}
}
3.springboot内置web容器的常用配置
参考类的属性进行配置:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# -----------------