@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")
1.配置文件解析
Spring Boot使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来。所以,我们要想把Spring Boot玩的溜,就要懂得如何开启各个功能模块的默认配置,这就需要了解Spring Boot的配置文件application.properties。
Spring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下。Sping Boot的全局配置文件的作用是对一些默认配置的配置值进行修改。
不存在 创建一个,application.properties提供自定义属性的支持,这样我们就可以把一些常量配置在这里:
test.baidu.url=http://www.baidu.com test.baidu.title=中国富强 test.baidu.s8how=nihao test.url=${test.baidu.show} , ${test.baidu.title},欢迎! #模板编码 spring.thymeleaf.encoding=UTF-8
然后直接在要使用的地方通过注解@Value(value=”${config.name}”)就可以绑定到你想要的属性上面
@Value(value = "${test.url}") private String gourl; @Value(value = "${test.baidu.title}") private String gotitle;
有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个Config.java类,顶部需要使用注解@ConfigurationProperties(prefix = “test.baidu”)来指明使用哪个
import org.springframework.boot.context.properties.ConfigurationProperties; /** * @author huxy * @备注 配置文件映射类 * @version V1.0 * @节点 */ @ConfigurationProperties(prefix="test.baidu") public class Config { private String url; private String title; private String show; public String getUrl(){ return url; } public void setUrl(String url){ this.url=url; } public String getTitle(){ return title; } public void setTitle(String title){ this.title=title; } public String getShow(){ return show; } public void setShow(String show){ this.show=show; } }
这里配置完还需要在spring Boot入口类加上@EnableConfigurationProperties并指明要加载哪个bean,如果不写Config.class,在bean类那边添加
@SpringBootApplication @EnableConfigurationProperties(Config.class) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
最后在Controller中引入Config使用即可,如下:
@Autowired private Config config;
使用:config.getShow() 即可
在application.properties中的各个参数之间也可以直接引用来使用:
test.baidu.url=http://www.baidu.com test.baidu.title=中国富强 test.baidu.s8how=nihao test.url=${test.baidu.show} , ${test.baidu.title},欢迎!
使用自定义配置文件
有时候我们不希望把所有配置都放在application.properties里面,这时候我们可以另外定义一个,这里我明取名为test.properties,路径跟也放在src/main/resources下面。
@Configuration @ConfigurationProperties(prefix = "com.md") @PropertySource("classpath:test.properties") public class ConfigTestBean { private String name; private String want; // 省略getter和setter }
如果你使用的是1.5以前的版本,那么可以通过locations指定properties文件的位置,这样:
@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")
但是1.5版本后就没有这个属性了,找了半天发现添加@Configuration和@PropertySource(“classpath:test.properties”)后才可以读取。
随机值配置
配置文件中${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦,例如 生成 int 值、long 值或者 string 字符串。
dudu.secret=${random.value} dudu.number=${random.int} dudu.bignumber=${random.long} dudu.uuid=${random.uuid} dudu.number.less.than.ten=${random.int(10)} dudu.number.in.range=${random.int[1024,65536]}
外部配置-命令行参数配置
Spring Boot是基于jar包运行的,打成jar包的程序可以直接通过下面命令运行:
java -jar xx.jar
可以以下命令修改tomcat端口号:
java -jar xx.jar --server.port=9090
可以看出,命令行中连续的两个减号--就是对application.properties中的属性值进行赋值的标识。
所以java -jar xx.jar --server.port=9090等价于在application.properties中添加属性server.port=9090。
如果你怕命令行有风险,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它。
实际上,Spring Boot应用程序有多种设置途径,Spring Boot能从多重属性源获得属性,包括如下几种:
根目录下的开发工具全局设置属性(当开发工具激活时为~/.spring-boot-devtools.properties)。
测试中的@TestPropertySource注解。
测试中的@SpringBootTest#properties注解特性。
命令行参数
SPRING_APPLICATION_JSON中的属性(环境变量或系统属性中的内联JSON嵌入)。
ServletConfig初始化参数。
ServletContext初始化参数。
java:comp/env里的JNDI属性
JVM系统属性
操作系统环境变量
随机生成的带random.* 前缀的属性(在设置其他属性时,可以应用他们,比如${random.long})
应用程序以外的application.properties或者appliaction.yml文件
打包在应用程序内的application.properties或者appliaction.yml文件
通过@PropertySource标注的属性源
默认属性(通过SpringApplication.setDefaultProperties指定).
这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties的属性。
配置文件的优先级
application.properties和application.yml文件可以放在以下四个位置:
外置,在相对于应用程序运行目录的/congfig子目录里。
外置,在应用程序运行的目录里
内置,在config包内
内置,在Classpath根目录
同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性
此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。
2.session 配置
springboot session默认时间是 1800秒。
配置文件添加:
server.session.timeout = 3000
工作原理:
http://blog.csdn.net/gaodebao1/article/details/51789188?readlog