Spring Boot程序默认从application.properties或者application.yaml读取配置,如何将配置信息外置,方便配置呢?
查询官网,可以得到下面的几种方案:
通过命令行指定
SpringApplication会默认将命令行选项参数转换为配置信息
例如,启动时命令参数指定:
java -jar myproject.jar --server.port = 9000
从命令行指定配置项的优先级最高,不过你可以通过setAddCommandLineProperties来禁用
SpringApplication.setAddCommandLineProperties(false).
外置配置文件
Spring程序会按优先级从下面这些路径来加载application.properties配置文件
- 当前目录下的/config目录
- 当前目录
- classpath里的/config目录
- classpath 跟目录
因此,要外置配置文件就很简单了,在jar所在目录新建config文件夹,然后放入配置文件,或者直接放在配置文件在jar目录
自定义配置文件
如果你不想使用application.properties作为配置文件,怎么办?完全没问题
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
或者
java -jar -Dspring.config.location=D:configconfig.properties springbootrestdemo-0.0.1-SNAPSHOT.jar
当然,还能在代码里指定
@SpringBootApplication
@PropertySource(value={"file:config.properties"})
public class SpringbootrestdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootrestdemoApplication.class, args);
}
}
按Profile不同环境读取不同配置
不同环境的配置设置一个配置文件,例如:
- dev环境下的配置配置在application-dev.properties中;
- prod环境下的配置配置在application-prod.properties中。
在application.properties中指定使用哪一个文件
spring.profiles.active = dev
当然,你也可以在运行的时候手动指定:
java -jar myproject.jar --spring.profiles.active = prod
参考:
1 参见Externalized Configuration
在spring boot学习1 时,知道spring boot会默认读取配置application.properties。那如果我们直接在application.properties添加自定义的配置项时,如何读取?或者不想把所有的配置都放在application.properties中,而是自定义一个properties文件时,又该如何读取呢?难道还需自己写代码加载读取配置文件吗?
注:下面代码是在spring boot 1.5.2版本下写的。
pom.xml
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.2.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
@Value
使用@Value注入写在application.properties中的配置项
application.properties
- logging.config=classpath:logback.xml
- logging.path=d:/logs
- ##tomcat set###
- server.port=80
- #session 超时时间
- server.session-timeout=60
- ###########
- hello=hello china!!!
- class.schoolName=china school
- class.className=second grade three class
- class.students[0].name=tom
- class.students[1].name=jack
在代码中@Value注入到属性中
- @Controller
- @RequestMapping("/")
- public class HelloController {
- public static Logger LOG = LoggerFactory.getLogger(HelloController.class);
- @Value("${hello}")
- private String hello;
- @Value("${class.schoolName}")
- private String schoolName;
@ConfigurationProperties
如果想把某种相关的配置,注入到某个配置类中,比如上面的application.properties中看到class.xx的配置项,想注入到ClassConfig配置类中- package com.fei.springboot.config;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- @Component("classConfig")
- @ConfigurationProperties(prefix="class")
- public class ClassConfig {
- private String schoolName;
- private String className;
- private List<StudentConfig> students = new ArrayList<StudentConfig>();
- public String getSchoolName() {
- return schoolName;
- }
- public void setSchoolName(String schoolName) {
- this.schoolName = schoolName;
- }
- public void setClassName(String className) {
- this.className = className;
- }
- public void setStudents(List<StudentConfig> students) {
- this.students = students;
- }
- public String getClassName() {
- return className;
- }
- public List<StudentConfig> getStudents() {
- return students;
- }
- }
我们看到代码中@ConfigurationProperties并没有指明properties,那默认的就是application.properties。那是否有地方指明properties的路径呢?在版本1.5.1之前可以@ConfigurationProperties(prefix="class",location="classpath:/customer.properties"),但是1.5.2版本没有location了,需要用@PropertySource("classpath:/student.properties")
- package com.fei.springboot.config;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.stereotype.Component;
- @Component("studentConfig")
- @ConfigurationProperties
- @PropertySource("classpath:/student.properties")
- public class StudentConfig {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
测试下
- package com.fei.springboot.controller;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- 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;
- import com.fei.springboot.config.ClassConfig;
- import com.fei.springboot.config.StudentConfig;
- @Controller
- @RequestMapping("/")
- public class HelloController {
- public static Logger LOG = LoggerFactory.getLogger(HelloController.class);
- @Value("${hello}")
- private String hello;
- @Value("${class.schoolName}")
- private String schoolName;
- @Autowired
- private ClassConfig classConfig;
- @Autowired
- private StudentConfig studentConfig;
- @RequestMapping(value="/hello")
- @ResponseBody
- public String hello(){
- System.out.println("=======使用@Value注入获取.....===========");
- System.out.println("hello="+hello+" schoolName=" + schoolName);
- System.out.println("======使用@ConfigurationProperties注入获取.....============");
- System.out.println("schoolName=" + classConfig.getSchoolName());
- System.out.println("className=" + classConfig.getClassName());
- System.out.println("student[0].name=" + classConfig.getStudents().get(0).getName());
- System.out.println("studentConfig...name=" + studentConfig.getName());
- return "hello";
- }
- }
- 2017-05-17 15:20:49.677 INFO 72996 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
- 2017-05-17 15:20:49.677 [http-nio-80-exec-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
- =======使用@Value注入获取.....===========
- hello=hello china!!! schoolName=china school
- ======使用@ConfigurationProperties注入获取.....============
- schoolName=china school
- className=second grade three class
- student[0].name=tom
- studentConfig...name=xiao hong