Spring Boot - 自定义属性及多环境配置

很多人选择Spring Boot就是因为抛弃了以往繁琐的XML配置,我们只需要在pom.xml文件中引入不同的模块,比如spring-boot-starter-web、spring-boot-starter-redis、spring-boot-starter-data-mongodb等,这些模块一般都已经有自己默认的配置,我们只需要在appication.properties中完成一些属性的配置就可以使用各模块了。

我们通常会在appication.properties中针对数据库连接、服务器参数等进行配置,与此同时我们也可以自定义一些参数放在这个文件中供系统使用。

自定义属性及加载

首先自定义属性如下

com.bluecoffee.space.author=bluecoffee    
com.bluecoffee.space.title=Spring Boot基础教程

通过@Value("${属性名}")注解来加载对应的配置属性,如下所示

    @Value("${com.bluecoffee.space.author}")
    private String blogAuthor;

    @Value("${com.bluecoffee.space.title}")
    private String blogTitle;

通过单元测试来验证一下

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RestApplication.class)
public class TestProperties {

    @Value("${com.bluecoffee.space.author}")
    private String blogAuthor;

    @Value("${com.bluecoffee.space.title}")
    private String blogTitle;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Test
    public void test1(){
        try{
            Assert.assertEquals("bluecoffee", blogAuthor);
            Assert.assertEquals("Spring Boot基础教程", blogTitle);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

使用随机数及自定义配置类

有时候我们需要我们的参数不是一个固定值,而是一个随机数(比如密钥)。Spring Boot的属性配置文件中可以通过${random}来产生int值、long值或者String字符串,来支持属性的随机值。

#随机字符串
com.bluecoffee.random.str=${random.value}
#随机int
com.bluecoffee.random.number=${random.int}
#随机long
com.bluecoffee.random.bigNumber=${random.long}
#10以内的随机数
com.bluecoffee.random.test1=${random.int(10)}
#10-20的随机数
com.bluecoffee.random.test2=${random.int[20,30]}

在这边配置中我们发现有一个特点,所有配置都是以"com.bluecoffee.random"开头的,我们也可以自定义一个配置类来进行声明,代码如下

@ConfigurationProperties(prefix = "com.bluecoffee.random")
public class RandomProperties {

    private String str;

    private Integer number;

    private Long bigNumber;

    private Integer test1;

    private Integer test2;

    // getter/setter方法省略
}

在上面一段代码中用@ConfigurationProperties(prefix = "com.bluecoffee.random")来约定了读取以"com.bluecoffe.random"开头的配置项,然后我们别忘了在RestApplication.java中使用@ EnableConfigurationProperties来开启读取配置文件的功能。

@SpringBootApplication
@EnableConfigurationProperties(RandomProperties.class)
public class RestApplication {

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

通过命令行来设置属性值

将Spring Boot应用打包后,我们可以使用java -jar xxx.jar --server.port=9001,通过使用--server.port属性来设置xxx.jar应用的端口为9001。

在命令行运行时,连续的两个减号--就是对application.properties中的属性值进行赋值的标识。所以,java -jar xxx.jar --server.port=9001命令,等价于我们在application.properties中添加属性server.port=9001,我们可通过删除该值或使用命令行来设置该值来验证。

通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,有时候并不安全,不用担心,Spring Boot也提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:

扫描二维码关注公众号,回复: 179890 查看本文章

SpringApplication.setAddCommandLineProperties(false)

多环境配置

我们知道在研发过程中,从开发-测试-上线,至少也有3个环境,实际上仅测试阶段可能还会有SIT、UAT阶段,然后每个环境的配置都不一样,比如数据库配置、Redis配置、分布式消息组件配置等。如果在打包环节来进行修改配置的话,非常容易出错。

对于多环境的配置,有非常多的项目构建工具,原理基本上都是通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,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=prod就会加载application-prod.properties配置文件内容

下面,以不同环境配置不同的服务端口为例,进行测试:

  • 针对各环境新建不同的配置文件application-dev.properties、application-test.properties、application-prod.properties

  • 在这三个文件均都设置不同的server.port属性,如:dev环境设置为1111,test环境设置为2222,prod环境设置为3333

  • application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置

测试不同配置的加载

执行java -jar xxx.jar,可以观察到服务端口被设置为1111,也就是默认的开发环境(dev)
执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为2222,也就是测试环境的配置(test)
执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为3333,也就是生产环境的配置(prod)
按照上面的实验,可以如下总结多环境的配置思路:

  • application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置
  • application-{profile}.properties中配置各个环境不同的内容
  • 通过java -jar xxx.jar --spring.profiles.active={}方式去激活不同环境的配置

猜你喜欢

转载自blog.csdn.net/suo082407128/article/details/77893815