springboot系列教程(三)——配置方式、多环境配置

Spring Boot 支持多种外部配置方式

这些方式优先级如下:

1、命令行参数
2、来自java:comp/env的JNDI属性
3、Java系统属性(System.getProperties())
4、操作系统环境变量
5、RandomValuePropertySource配置的random.*属性值
6、jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7、jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
8、jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9、jar包内部的application.properties或application.yml(不带spring.profile)配置文件
10、@Configuration注解类上的@PropertySource
11、通过SpringApplication.setDefaultProperties指定的默认属性命令行参数

命令行参数

通过*java -jar app.jar - -name=”Spring” - -server.port=9090方式来传递参数。

参数用–xxx=xxx的形式传递。

可以使用的参数可以是我们自己定义的,也可以是Spring Boot中默认的参数。

很多人可能会关心如web端口如何配置这样的问题,这些都是Spring Boot中提供的参数,部分可用参数如下:

LOGGING
logging.path=/var/logs
logging.file=myapp.log
logging.config= # location of config file (default classpath:logback.xml for logback)
logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080
server.address= # bind to a specific NIC
server.session-timeout= # session timeout in seconds
server.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alpha
server.context-path= # the context path, defaults to '/'
server.servlet-path= # the servlet path, defaults to '/'

更多常见的应用属性请浏览这里

注意:命令行参数在app.jar的后面!

可以通过SpringApplication.setAddCommandLineProperties(false)禁用命令行配置。

Java系统属性

注意Java系统属性位置java -Dname=”isea533” -jar app.jar,可以配置的属性都是一样的,优先级不同。

例如java -Dname=”isea533” -jar app.jar –name=”Spring!”中name值为Spring!

操作系统环境变量

配置过JAVA_HOME的应该都了解这一个。

这里需要注意的地方,有些OS可以不支持使用.这种名字,如server.port,这种情况可以使用SERVER_PORT来配置。

具体名字如何匹配,看本文后面。

RandomValuePropertySource系统中用到随机数的地方,例如:

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

random.int*支持value参数和,max参数,当提供max参数的时候,value就是最小值。

应用配置文件(.properties或.yml)

在配置文件中直接写:

name=Isea533
server.port=8080

.yml格式的配置文件如:

name: Isea533
server:
    port: 8080

当有前缀的情况下,使用.yml格式的配置文件更简单。关于.yml配置文件用法请看这里

注意:使用.yml时,属性名的值和冒号中间必须有空格,如name: Isea533正确,name:Isea533就是错的。

属性配置文件的位置

spring会从classpath下的/config目录或者classpath的根目录查找application.properties或application.yml。

/config优先于classpath根目录

@PropertySource

这个注解可以指定具体的属性配置文件,优先级比较低。

SpringApplication.setDefaultProperties

例如:

SpringApplication application = new SpringApplication(Application.class);
Map<String, Object> defaultMap = new HashMap<String, Object>();
defaultMap.put("name", "Isea-Blog");
//还可以是Properties对象
application.setDefaultProperties(defaultMap);
application.run(args);

应用(使用)属性

@Value(“${xxx}”)  这种方式是最简单的,通过@Value注解可以将属性值注入进来。
@ConfigurationProperties  Spring Boot 可以方便的将属性注入到一个配置对象中。例如:

maven引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

yml

my:
  name: Isea2
  port: 8880
  servers[0]: dev.bar.com
  servers[1]: foo.bar.com

对应对象:

package com.example.demo.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :小虎
 * @date :2017/12/22
 */
@Component
@ConfigurationProperties(prefix="my")
public class Config {
    private String name;
    private Integer port;
    private List<String> servers = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public List<String> getServers() {
        return servers;
    }

    public void setServers(List<String> servers) {
        this.servers = servers;
    }

    @Override
    public String toString() {
        return "Config{" +
                "name='" + name + '\'' +
                ", port=" + port +
                ", servers=" + servers +
                '}';
    }
}

新建HelloController

package com.example.demo.controller;

import com.example.demo.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :小虎
 * @date :2017/12/22
 */


@RestController
public class HelloController {

    @Autowired
    private Config config;

    @Autowired
    private Environment environment;

    @Value("${my.name}")
    private String name;

    @RequestMapping("/")
    public String hello(){
        return config.toString();
    }
    @RequestMapping("/name")
    public String name(){
        return name;
    }
    @RequestMapping("/env")
    public String env(){
        return environment.getProperty("my.port");
    }
}

Spring Boot 会自动将prefix=”my”前缀为my的属性注入进来。

Spring Boot 会自动转换类型,当使用List的时候需要注意在配置中对List进行初始化!

Spring Boot 还支持嵌套属性注入,例如:

name=isea533
jdbc.username=root
jdbc.password=root

对应的配置类:

@ConfigurationProperties
public class Config {
    private String name;
    private Jdbc jdbc;
    class Jdbc {
        private String username;
        private String password;
        //getter...
    }

    public Integer gePort(){
        return this.port;
    }
    public Jdbc getJdbc() {
        return this.jdbc;
    }
}

jdbc开头的属性都会注入到Jdbc对象中。

在@Bean方法上使用@ConfigurationProperties

例如:

@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
    ...
}

Spring Boot 会将foo开头的属性按照名字匹配注入到FooComponent对象中。

属性占位符

例如:

app.name=MyApp
app.description=${app.name} is a Spring Boot application

可以在配置文件中引用前面配置过的属性(优先级前面配置过的这里都能用)。

通过如${app.name:默认名称}方法还可以设置默认值,当找不到引用的属性时,会使用默认的属性。

由于${}方式会被Maven处理。如果你pom继承的spring-boot-starter-parent,Spring Boot 已经将maven-resources-plugins默认的方式改为了@ @方式,例如@name@。如果你是引入的Spring Boot,你可以修改使用其他的分隔符

通过属性占位符还能缩短命令参数

例如修改web默认端口需要使用–server.port=9090方式,如果在配置中写上:

server.port=${port:8080}

那么就可以使用更短的–port=9090,当不提供该参数的时候使用默认值8080。

属性名匹配规则

例如有如下配置对象:

@Component
@ConfigurationProperties(prefix="person")
public class ConnectionSettings {

    private String firstName;

}

firstName可以使用的属性名如下:

person.firstName,标准的驼峰式命名
person.first-name,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用
PERSON_FIRST_NAME,大写下划线形式,建议在系统环境变量中使用
属性验证

可以使用JSR-303注解进行验证,例如:

@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {

    @NotNull
    private InetAddress remoteAddress;

    // ... getters and setters

}

多环境配置

我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:

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

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

如:spring.profiles.active=test就会加载application-test.properties配置文件内容

源码地址

https://github.com/itmybaby/springboot/tree/master/study3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值