SpringBoot configuration file is bound to javabean

1. In some businesses, the configuration of the class needs to be written into the configuration file, instead of hard-coded configuration in the code.
2. So you need to read the configuration file (yaml, properties) into the Java Bean.
3. This article uses oss object storage configuration as demo. There are two ways to initialize the configuration file.

-----------------
1. yml configuration file

server:
  port: 8888

spring:
  application:
    name: hello-world

# oss object storage configuration
oss-constant:
  endpoint: secret
  access-key: key
  secret-key: key
  bucket: bucket

2. Binding method 1

Use the @Value annotation to inject the value of the configuration file.
Benefits: It can be configured selectively. Can class name. Attribute.
Disadvantages: The @Value(value="xxx") prefix is ​​hard-coded, and it is inconvenient to modify the project after it is packaged.

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;

/**
 * Load oss ​​object storage configuration
 * java project 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() {
        // Initialize the setting value
        ENDPOINT = this.endpoint;
        ACCESS_KEY_ID = this.accessKeyId;
        ACCESS_KEY_SECRET = this.accessKeySecret;
        BUCKET_NAME = this.bucketName;
    }

}

3. Binding method 2

3.1 Dependence.

    <dependencies>

		<!-- Prompt for attribute assembly -->
        <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>
                        <!-- Exclude attribute assembly prompt plug-in when packaging -->
                        <configuration>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3.2 The attribute assembly function provided in boot.

The prefix in yaml corresponds to the prefix of ConfigurationProperties below.
The attributes in yaml correspond to the attributes of the entity class. (The horizontal bar in yaml can turn hump)

package top.bitqian.hello.constant;

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

/**
 * This Java class is bound to the configuration file.
 * java project www.fhadmin.org
 * @date 2021/3/20 09:36
 */

/*
  1. Binding method 1:
  @Component
  @ConfigurationProperties(prefix = "oss-constant")

  2. Binding method 2:
  Entity configuration class, @ConfigurationProperties(prefix = "oss-constant")
  Turn on the configuration file (yml, properties) binding on the configuration class:
  @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;
}

If you get it, just get it from the container. The value in the configuration file will be automatically assembled into the Java Bean.


Guess you like

Origin blog.51cto.com/14622073/2667985