springboot配置文件的读取方式

最近在做项目的时候,因为需求原因,要定义几个常量,但是这几个常量有可能改动,为了尽量不改动代码,就想能不能放到SpringBooot的配置文件中呢,毕竟配置文件先于SpringBoot的核心业务类加载,因此配置在配置文件中的属性便相当于有了static属性。具体的解决方案分为两大类:
1、自定义配置文件类进行读取;
bootstrap.properties中的属性内容如下:

##spring.application.name=spring-cloud-zuul
##server.port=8002

spring.cloud.config.name=zj-cfg4zuul
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
#网络前缀;
NET-PREFIX.DOMAIN=http://localhost:8006/
#------内部URL------;
#Excel上传的URL;
NET-PREFIX.EXCELUPLOADURL=uploadExcel
#身份认证的URL;
NET-PREFIX.AUTHENTICATIONURL=spring-cloud-producer/mongo/auth/doGetAuthenticationInfo
#权限认证的URL;
NET-PREFIX.AUTHORIZATIONURL=spring-cloud-producer/mongo/auth/doGetAuthorizationInfo
#保存Session的URL;
NET-PREFIX.SAVEUSERSESSIONURL=spring-cloud-producer/mongo/auth/saveUserSessionWithJsessionid
#移除Sessin的URL;
NET-PREFIX.REMOVEUSERSESSIONURL=spring-cloud-producer/mongo/auth/removeUserSessionFromDB
#师傅对头部信息敏感;
zuul.sensitive-headers=false

创建实体类MyConfig。
注意:springboot1.5版本以下@ConfigurationProperties有两个属性locations(指定配置文件的所在位置),

当前的文件路径如下图所示:
springboot配置文件的读取方式_第1张图片


package com.neo.webConfig;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
 * 自定义的配置文件类;
 * @author xdsm
 *
 */
@Component
@ConfigurationProperties(prefix = "NET-PREFIX")
@PropertySource(value = "classpath:bootstrap.properties")
public class MyConfig {
    //域名;
    private String DOMAIN;
    //Excel上传的URL;
    private String EXCELUPLOADURL;
    //身份认证的URL;
    private String AUTHENTICATIONURL;
    //权限认证的URL;
    private String AUTHORIZATIONURL;
    //Session保存的URL;
    private String SAVEUSERSESSIONURL;
    //Session移除的URL;
    private String REMOVEUSERSESSIONURL;

    public String getDOMAIN() {
        return DOMAIN;
    }
    public void setDOMAIN(String dOMAIN) {
        DOMAIN = dOMAIN;
    }
    public String getEXCELUPLOADURL() {
        return EXCELUPLOADURL;
    }
    public void setEXCELUPLOADURL(String eXCELUPLOADURL) {
        EXCELUPLOADURL = eXCELUPLOADURL;
    }
    public String getAUTHENTICATIONURL() {
        return AUTHENTICATIONURL;
    }
    public void setAUTHENTICATIONURL(String aUTHENTICATIONURL) {
        AUTHENTICATIONURL = aUTHENTICATIONURL;
    }
    public String getAUTHORIZATIONURL() {
        return AUTHORIZATIONURL;
    }
    public void setAUTHORIZATIONURL(String aUTHORIZATIONURL) {
        AUTHORIZATIONURL = aUTHORIZATIONURL;
    }
    public String getSAVEUSERSESSIONURL() {
        return SAVEUSERSESSIONURL;
    }
    public void setSAVEUSERSESSIONURL(String sAVEUSERSESSIONURL) {
        SAVEUSERSESSIONURL = sAVEUSERSESSIONURL;
    }
    public String getREMOVEUSERSESSIONURL() {
        return REMOVEUSERSESSIONURL;
    }
    public void setREMOVEUSERSESSIONURL(String rEMOVEUSERSESSIONURL) {
        REMOVEUSERSESSIONURL = rEMOVEUSERSESSIONURL;
    }
    @Override
    public String toString() {
        return "MyConfig [DOMAIN=" + DOMAIN + ", EXCELUPLOADURL=" + EXCELUPLOADURL + ", AUTHENTICATIONURL="
                + AUTHENTICATIONURL + ", AUTHORIZATIONURL=" + AUTHORIZATIONURL + ", SAVEUSERSESSIONURL="
                + SAVEUSERSESSIONURL + ", REMOVEUSERSESSIONURL=" + REMOVEUSERSESSIONURL + "]";
    }


}

   2、非自定义配置文件类进行读取;

   (1)@Value("${键名}")
RestController
public class HelloController {

    @Value("${service.name}")
    private String name;

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello spring boot! " + name;
    }
}

(2)使用Environment,env.getProperty(“键名”)

@RestController
public class HelloController {
    @Autowired
    private Environment env;

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello spring boot! " + env.getProperty("service.name");
    }
}

综上所述,获取SpringBoot有两大类,三种方法。如果有代码洁癖,非要对属性进行分类管理的话,推荐使用自定义属性类的方法,这样可以对属性进行分类管理。

你可能感兴趣的:(J2EE)