springboot2.1系列之第二篇 属性配置

1.开发之前

属性配置是开发必不可少的,配置的外部化有利于我们增删改查配置,同时可以使我们应对不同的运行环境,spring boot可以使用property文件,yaml文件,系统属性,命令行参数。这里使用property文件,总结一下所有可能出现的属性配置问题。依托于第一篇,我们把代码重新整理一下:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApp {

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

这里把启动应用类放到根包下,使用新的 @SpringBootApplication注解代替**@EnableAutoConfiguration**,新的注解集 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan于一身,自带根包路径扫描,启用自动化配置。

我们在resource目录下添加 application.properties属性文件,添加一个自己的属性配置:

hello=hello spring boot2
2.代码中访问属性配置

在代码里访问外部属性配置,一般可以使用 @value直接将属性配置注入到代码的成员变量,或者使用 @ConfigurationProperties将一类属性配置绑定到同一个对象,当然属性配置的访问须在spring的托管下。

1使用@value

package com.hello.properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class HelloProperties {

    @Value(value = "${hello}")
    private String hello;

    public String getHello() {
        return hello;
    }
}

如代码所示,可以直接使用 @value注解给成员变量hello注入属性值,这里使用 @Component交由springboot扫描实例化,使用其它的spring组件注解等同。

2使用@ConfigurationProperties

使用此注解需要添加相关依赖:

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

这个功能比较强大,而且支持属性嵌套,可以很好的支持面向对象的类和类成员变量,我们创建一个嵌套的java类。

package com.hello.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "global")
public class GlobalProperties {

    private DemoProperties demo = new DemoProperties();

    public DemoProperties getDemo() {
        return demo;
    }
    public void setDemo(DemoProperties demo) {
        this.demo = demo;
    }
}
package com.hello.properties;

public class DemoProperties {

    private String name;
    private String date;
}
hello=hello spring boot2

global.demo.name=springboot2
global.demo.date=2019-11-14

global作为大前缀下,我们把以demo为前缀的同样可以映射到一个实体类DemoProperties ,将demo作为GlobalProperties的属性注入进来,调用的地方统一使用GlobalProperties实例即可。注意成员变量命名要和属性配置一致,其次要注意GlobalProperties也需要被spring扫描实例化才生效。下面在controller中验证下:

package com.hello.controller;

import com.hello.properties.DemoProperties;
import com.hello.properties.GlobalProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private GlobalProperties globalProperties;
	
	@RequestMapping("/welcome")
    String welcomme() {
        DemoProperties demo = globalProperties.getDemo();
        System.out.println(demo);
        return demo.toString();
    }
}

在这里插入图片描述

3.属性配置针对不同的环境
application-{profile}.properties

可以按照上述文件命名方式区分多个环境,并且在application.properties文件中指定要生效的环境配置:

spring.profiles.active=dev

这样我们可以将不同环境的属性配置放到对应的属性配置文件里,这个和maven的profile是一个性质的,就是支持不同环境使用不同的属性配置。

4.日志相关属性配置

通常使用的日志组件有log4j,log4j2,logback等,如何在一个日志配置文件中应对不同的环境?比如集成测试环境和生产环境日志输出文件目录不一致,日志输出级别不一致等问题。因为日志的初始化是在应用加载早期完成的,所以传统的属性配置在日志配置文件中是访问不到的。下面以logback为例:

为了是spring可以更好的控制日志初始化,建议使用logback-spring.xml命名日志配置文件,为了扩展支持日志配置文件中的变量,spring会加载特定属性键值到system property。比如我们在属性文件中声明:

logging.path=/app/demo

spring会添加 LOG_PATH 到system property,然后我们就可以直接在配置文件中使用**${LOG_PATH}**,再结合3中的不同环境的属性配置,就可以在日志配置中动态指定日志目录了。

<file>${LOG_PATH}/info.log</file>

同时,我们也可以在日志配置文件中使用springProfile用来区分不同的环境配置。

<springProfile name="dev">
	<root level="debug">
		<appender-ref ref="CONSOLE" />
		<appender-ref ref="INFO_FILE" />
	</root>
</springProfile>
5.结束

属性配置是开发中必不可少的,本文分享一下相关常规属性配置以及日志相关属性的介绍。官网的资料非常详尽,有问题我们可以一查官网文档,二看源代码。
比如,正常使用logback时,我们可以使用 :- 分隔符来指定属性的默认值,在springboot里我们需要使用 : 分隔符。

原创文章 17 获赞 13 访问量 7617

猜你喜欢

转载自blog.csdn.net/weixin_43275277/article/details/103062176