Spring Boot加载配置文件

问题1:Spring如何加载配置,配置文件位置?

1、默认位置:

    Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:

  • 当前目录下的/config子目录,
  • 当前目录。
  • 一个classpath下的/config包
  • classpath根路径(root)

这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)。并且,如果存在多个重名的application.properties。

注:Spring-boot配置文件的加载,先在与jar同级下查找,如果没有就去同级的config下查找;如果再没有,就在jar包中去查找相应的配置文件,如果再没有,就去jar包中的config下去查找。当查找到对应配置片段时,采用增量替换的方式来进行替换

2、自定义位置:

    如果不喜欢application.properties这个文件名或者需要自定义配置文件位置,在启动Spring应用的时候,可以传入一个spring.config.location参数指定配置文件位置,

例如:

java -jar xxxxx.jar   --spring.config.location=classpath:/default.properties,classpath:/override.properties

上述例子加载了两个配置文件,分别位于根目录下的:default.properties,override.properties。

 

问题2:配置文件如何加载到类文件中?

配置文件application.properties中配置属性:

test.name = wahaha
test.age = 27
test.tel = 18800118888

1、@Component和@Value("${"xxx"}")

类文件:

@Component
public class Configurations {
    
    @Value("${test.name}")
    private String name;

    @Value("${test.age}")
    private String age;

    @Value("${test.tel}")
    private Long tel;

    // getter and setter
}

在类域属性上通过@Value("${xxx}")指定关联属性,Spring Application会自动加载。

启动类:

@RestController
@SpringBootApplication
public class ApplicationStarter {

    @Autowired
    private Configuration configuration;

    @RequestMapping("/")
    public Map<String, Object> sayHello() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("name", configuration.getName());
        result.put("age", configuration.getAge());
        result.put("tel", configuration.getTel());
        return result;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationStarter.class, args);
    }
}

运行,调用localhost:8080/会看到返回结果是:

{"name":"wahaha","tel":18800118888,"age":"27"}

2、@ConfigurationProperties(prefix = "test")

上述方法,手动书写@Value还是比较繁重的工作,好在Spring Boot提供了更简洁的方式。@ConfigurationProperties(prefix = "test")。prefix指定了配置文件的前缀为test,并且按照属性名进行自动匹配,例如:test.name属性值会自动加载到private String name域中。

@Component
@ConfigurationProperties(prefix = "test")
public class Configuration {

    private String name;

    private String age;

    private Long tel;

    // setter getter
}

PS:locations还能够指定自定义的配置文件位置,这里就不多说了。

@ConfigurationProperties(prefix = "test", locations = "classpath:xxxx.properties")

 

问题3:如何根据线上环境和线下环境加载不同的配置?如何加载多个配置文件?

1、Profiles:

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。在配置文件中,用spring.profiles.active属性来指定特定环境的配置文件。在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境
     

    至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。例如:

application.properties配置:spring.profiles.active = dev即指定application-dev.properties会被加载。


2、通过启动参数指定运行环境。

通过命令行启动:

  java -jar xxxxx.jar   --spring.profiles.active=prod。

    上述指定为prod环境,则spring application会自动根据application-{profile}.properties的格式找到application-prod.properties文件加载。

3、加载多个自定义文件。

例如:

 spring.profiles.active = dev,database

等于告诉Spring Boot加载application-dev.properties和application-database.properties文件,从而实现多个配置文件的加载。

参见:https://my.oschina.net/wangyuefive/blog/704615#h3_4

posted @ 2017-08-15 23:09  moonandstar08  阅读(54221)  评论(0编辑  收藏  举报