SpringBoot2.0实现自定义properties配置文件与JavaBean映射

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/myNameIssls/article/details/81545710

SpringBoot2.0实现自定义properties配置文件与JavaBean映射

博文需求分析

SpringBoot1.4及之前的版本中,要实现自定义properties配置文件与JavaBean的映射,只需要在配置文件对应的JavaBean上增加@ConfigurationProperties(locations="classpath:config.properties", prefix="config.author")这个注解,并在项目启动类上增加@EnableConfigurationProperties启动配置即可。但是在之后的版本中,@ConfigurationProperties注解不再提供locations属性,所以,无法实现映射。本篇博文就是为解决这个问题的

解决方案

解决这个问题,只需要在配置文件对应的JavaBean上增加@Component@ConfigurationProperties(prefix = "config.author")@PropertySource(value = "classpath:config.properties", encoding = "UTF-8") 这三个注解即可,也不需要在项目启动类上增加@EnableConfigurationProperties这个注解。

示例代码:

引入相关依赖

<dependencies>
    <!-- 用于简化JavaBean -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Boot基础依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- 配置文件自动映射 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- web相关依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 热部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

</dependencies>

增加配置文件

本篇博文将配置文件命名为:config.properties

config.author.name=Tyrone
config.author.age=28
config.author.addr=北京市

编写JavaBean映射类

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

import lombok.Data;

@Data
@Component
@ConfigurationProperties(prefix = "config.author")
@PropertySource(value = "classpath:config.properties", encoding = "UTF-8") 
public class ConfigProperties {
    private String name;
    private String age;
    private String addr;
}

说明:
@Data用来简化JavaBean体态,使用该注解实现JavaBeangetset等方法。

编写主启动类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import cn.tyrone.springboot.example.properties.ConfigProperties;

@SpringBootApplication
public class Application implements CommandLineRunner{

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired private ConfigProperties configProperties;

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

    @Override
    public void run(String... args) throws Exception {

        String config = "config.author.name: " + configProperties.getName()
                    + ", config.author.age:" + configProperties.getAge() 
                    + ", config.author.addr:" + configProperties.getAddr();

        log.info("SpringBoot2.0实现自定义properties配置文件与JavaBean映射:" + config);
    }

}

测试

启动本程序后,观察控制台,可以看到自定义properties配置文件与JavaBean映射结果,如下:

2018-08-09 22:51:48.100  INFO 18798 --- [  restartedMain] ication$$EnhancerBySpringCGLIB$$81e6d45a : SpringBoot2.0实现自定义properties配置文件与JavaBean映射:config.author.name: Tyrone, config.author.age:28, config.author.addr:北京市

其它解决方案

SpringBoot默认的配置文件是application.propertiesapplication.yml,如果把JavaBean属性相关配置写进去,也是可以实现自动映射的,但是由于application.propertiesapplication.yml通常用来配置系统环境相关的配置,所以不建议将一些与系统环境不相关的属性配置到这个文件中。

源代码地址:
https://github.com/myNameIssls/springboot-study/tree/master/springboot-example

猜你喜欢

转载自blog.csdn.net/myNameIssls/article/details/81545710
今日推荐