Spring Boot读取配置的几种方式

2019-02-28  本文已影响0人  yellow_han

1、@Value注解读取方式

application.properties或者yml
beam:
  wxapp:
    swagger-open: true
    file-upload-path: /upload

BeamWxappProperties
package com.gizhi.beam.config.properties;
import java.io.File;

import static com.gizhi.beam.core.utils.ToolUtil.getTempPath;
import static com.gizhi.beam.core.utils.ToolUtil.isEmpty;
@Component
public class BeamWxappProperties {

    @Value("${beam.wxapp.fileUploadPath}")
    private String fileUploadPath;

    @Value("${beam.wxapp.swaggerOpen}")
    private Boolean swaggerOpen;

    private Boolean haveCreatePath = false;


    public String getFileUploadPath() {
        //如果没有写文件上传路径,保存到临时目录
        if (isEmpty(fileUploadPath)) {
            return getTempPath();
        } else {
            //判断有没有结尾符,没有得加上
            if (!fileUploadPath.endsWith(File.separator)) {
                fileUploadPath = fileUploadPath + File.separator;
            }
            //判断目录存不存在,不存在得加上
            if (!haveCreatePath) {
                File file = new File(fileUploadPath);
                file.mkdirs();
                haveCreatePath = true;
            }
            return fileUploadPath;
        }
    }

    public void setFileUploadPath(String fileUploadPath) {
        this.fileUploadPath = fileUploadPath;
    }
}

2、@ConfigurationProperties注解读取方式

package com.gizhi.beam.config.properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;

import static com.gizhi.beam.core.utils.ToolUtil.getTempPath;
import static com.gizhi.beam.core.utils.ToolUtil.isEmpty;


@Configuration
@ConfigurationProperties(prefix = BeamWxappProperties.BEAM_WXAPP_PREFIX)
public class BeamWxappProperties {

    public static final String BEAM_WXAPP_PREFIX = "beam.wxapp";




    private String fileUploadPath;

    private Boolean swaggerOpen;



    private Boolean haveCreatePath = false;


    public String getFileUploadPath() {
        //如果没有写文件上传路径,保存到临时目录
        if (isEmpty(fileUploadPath)) {
            return getTempPath();
        } else {
            //判断有没有结尾符,没有得加上
            if (!fileUploadPath.endsWith(File.separator)) {
                fileUploadPath = fileUploadPath + File.separator;
            }
            //判断目录存不存在,不存在得加上
            if (!haveCreatePath) {
                File file = new File(fileUploadPath);
                file.mkdirs();
                haveCreatePath = true;
            }
            return fileUploadPath;
        }
    }

    public void setFileUploadPath(String fileUploadPath) {
        this.fileUploadPath = fileUploadPath;
    }
}

3、读取指定文件

dbconfig.properties

db.username=root
db.password=test

DbConfig (通过Properties加载文件)

package com.gizhi.beam.constant;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @description:
 * @author: hs
 * @create: 2019-02-28 10:18:36
 **/
public class DbConfig {

    public static String USERNAME;

    public static String PASSWORD;

    static{
        Properties prop = new Properties();
        InputStream in = DbConfig.class.getResourceAsStream("/dbconfig.properties");
        try {
            prop.load(in);
            USERNAME = prop.getProperty("db.username").trim();
            PASSWORD = prop.getProperty("db.password").trim();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

DbConfig1 (@PropertySource+@Value注解读取方式)

package com.gizhi.beam.constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"/db-config.properties"})
public class DbConfig1 {


    @Value("${db.username}")
    private String username;

    @Value("${db.password}")


    private String password;


    public String getUsername() {

        return username;

    }


    public void setUsername(String username) {

        this.username = username;


    }


    public String getPassword() {

        return password;

    }


    public void setPassword(String password) {

        this.password = password;

    }


}

DbConfig2 (@PropertySource+@ConfigurationProperties注解读取方式)

package com.gizhi.beam.constant;

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

@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = {"/db-config.properties"})
public class DbConfig2 {


    private String username;


    private String password;


    public String getUsername() {

        return username;

    }


    public void setUsername(String username) {


        this.username = username;


    }


    public String getPassword() {


        return password;


    }


    public void setPassword(String password) {


        this.password = password;


    }


}

注意

@PropertySource不支持yml文件读取。

4、关注我的公众号,互相学习。

image.png
上一篇下一篇

猜你喜欢

热点阅读