SpringBoot 配置文件绑定到 javabean

1. 在某些业务中,需要将类的配置写到配置文件中, 不在代码中写死配置。
2. 所以需要读取配置文件(yaml, properties) 读取到Java Bean中。
3. 本文以oss对象存储配置为demo。两种方式初始化配置文件。

-----------------
1. yml配置文件

server:
  port: 8888

spring:
  application:
    name: hello-world

# oss对象存储配置
oss-constant:
  endpoint: secret
  access-key: key
  secret-key: key
  bucket: bucket

2. 绑定方式1

使用@Value注解, 注入配置文件的值。
好处:能够选择性的配置。能够类名.属性。
坏处:写死了@Value(value=“xxx”) 前缀,项目打包后了不方便修改。

package top.bitqian.hello.constant;

import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 加载oss对象存储配置
 * java项目www.fhadmin.org
 * @date 2021/3/20 09:30
 */
 
@Data
@Component
public class OssLoadConstant implements InitializingBean {

    @Value("${oss-constant.endpoint}")
    private String endpoint;

    @Value("${oss-constant.access-key}")
    private String accessKeyId;

    @Value("${oss-constant.secret-key}")
    private String accessKeySecret;

    @Value("${oss-constant.bucket}")
    private String bucketName;

    public static String ENDPOINT;
    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;
    public static String BUCKET_NAME;

    @Override
    public void afterPropertiesSet() {
        // 初始化设置值
        ENDPOINT = this.endpoint;
        ACCESS_KEY_ID = this.accessKeyId;
        ACCESS_KEY_SECRET = this.accessKeySecret;
        BUCKET_NAME = this.bucketName;
    }

}

3. 绑定方式2

3.1 依赖.

    <dependencies>

		<!-- 属性装配提示 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- 打包时排除 属性装配提示插件 -->
                        <configuration>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3.2 boot 中提供的属性装配功能。

yaml中的前缀和下面的ConfigurationProperties的prefix对应。
yaml中的属性和实体类属性对应。(yaml中的横杠可以转驼峰)

package top.bitqian.hello.constant;

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

/**
 * 此Java类与配置文件绑定了.
 * java项目www.fhadmin.org
 * @date 2021/3/20 09:36
 */

/*
  1. 绑定方法1:
  @Component
  @ConfigurationProperties(prefix = "oss-constant")

  2. 绑定方法2:
  实体配置类, @ConfigurationProperties(prefix = "oss-constant")
  在配置类上开启和配置文件(yml, properties)绑定 :
  @EnableConfigurationProperties(value = {OssConstantConfig.class})
 */


@Data
@Component
@ConfigurationProperties(prefix = "oss-constant")
public class OssConstantConfig {

    private String endpoint;

    private String accessKey;

    private String secretKey;

    private String bucket;
}

获取的话,从容器中获取就行了。配置文件中的值会自动装配到Java Bean中。


猜你喜欢

转载自blog.51cto.com/14622073/2667985