一、配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里)
package com.my.study.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.my.study.model.Student; @RestController @SpringBootApplication @RequestMapping("/user") public class UserController { @Value("${test.stuName}") private String stuName; @Value("${test.stuSex}") private String stuSex; @RequestMapping("/test") public Object sayHello() { Student student = new Student(); student.setStuName(stuName); student.setStuSex(stuSex); return student ; } }
启动后在浏览器直接输入http://localhost:18080/user/test,就直接打印出配置文件中的配置内容。
二、绑定对象bean调用
有时候属性太多了,一个个绑定到属性字段上太累,官方提倡绑定一个对象的bean,这里我们建一个ConfigBean.java类,顶部需要使用注解@ConfigurationProperties(prefix = “test”)来指明使用哪个
注意:类中一定要有get set 和无参的构造方法方法,否则参数无法绑定,
package com.my.study.model; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix ="test") public class ConfigBean { private String stuName; private String stuSex; /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "ConfigBean [stuName=" + stuName + ", stuSex=" + stuSex + "]"; } /** * @return the stuName */ public String getStuName() { return stuName; } /** * @param stuName the stuName to set */ public void setStuName(String stuName) { this.stuName = stuName; } /** * @return the stuSex */ public String getStuSex() { return stuSex; } /** * @param stuSex the stuSex to set */ public void setStuSex(String stuSex) { this.stuSex = stuSex; } }
此时配置完还需要在spring Boot入口类加上@EnableConfigurationProperties并指明要加载哪个bean,如果不写RandomProperties.class,在bean类那边添加@Configuration或者@Component
package com.my.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import com.my.study.model.ConfigBean; @SpringBootApplication @EnableConfigurationProperties(ConfigBean.class) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
最后在Controller中引入ConfigBean使用即可,如下:
package com.my.study.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.my.study.model.ConfigBean; @RestController @SpringBootApplication @RequestMapping("/user") public class UserController { @Autowired ConfigBean configBean; @RequestMapping("/test") public Object sayHello() { return configBean; } }
三、参数间引用
在application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:
test.stuName = Joe test.stuSex = gril test.allInfo = stuName:${test.stuName} stuSex:${test.stuSex}
四、使用自定义新建的配置文件
package com.my.study.model; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @ConfigurationProperties(prefix ="test") @PropertySource("classpath:config/my.properties") public class Config { private String stuName; private String stuSex; /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "ConfigBean [stuName=" + stuName + ", stuSex=" + stuSex + "]"; } /** * @return the stuName */ public String getStuName() { return stuName; } /** * @param stuName the stuName to set */ public void setStuName(String stuName) { this.stuName = stuName; } /** * @return the stuSex */ public String getStuSex() { return stuSex; } /** * @param stuSex the stuSex to set */ public void setStuSex(String stuSex) { this.stuSex = stuSex; } }
主要就是加了一个注解:@PropertySource("classpath:config/my.properties")
五、配置文件优先级
application.properties和application.yml文件可以放在一下四个位置:
- 外置,在相对于应用程序运行目录的/congfig子目录里。
- 外置,在应用程序运行的目录里
- 内置,在config包内
- 内置,在Classpath根目录
同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,
此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.yml里面的属性就会被application.properties里的属性覆盖。
本地配置是一个存在后不读取其他的
除非定义了profile,或者使用了spring config(其实也是一次性读取多个聚合结果)
bootstrap.properties优先于application.prperties
properties优先于yml
resources/config优先于resources/
六.随机值配置
配置文件中${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的属性。
详细参考:http://tengj.top/2017/02/28/springboot2/