JAVA语言之Spring boot 工具类静态属性注入及多环境配置
小标 2018-07-24 来源 : 阅读 775 评论 0

摘要:本文主要向大家介绍了JAVA语言之Spring boot 工具类静态属性注入及多环境配置,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言之Spring boot 工具类静态属性注入及多环境配置,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理。但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此开发好程序后,总是要修改一下MongoDB服务器的IP才能提交代码,这样很是不方便。

    private static final String PUBCHAT_HOST = "127.0.0.2";//  private static final String PUBCHAT_HOST = "PROD_MONGO_SERVER_IP";

由于没有使用spring-boot自带的 spring-boot-starter-data-mongodb ,而是使用 mongo-java-driver 访问MongoDB,因此在程序中需要定义一些访问MongoDB的配置,比如服务器地址、IP端口、数据库名……使用一个工具类的静态变量声明这些配置信息,配置信息的值保存在application.yml 配置文件中。通过 @ConfigurationProperties 注入。

静态工具类定义

属性是静态的:

private static  String chat_username;

然后通过非静态的 set方法注入:

    @Value("${mongo.config.username}")

    public void setChat_username(String chat_username) {

    MongoConfig.chat_username = chat_username;

    }

其他类通过公有的静态get方法获取属性:

    public static String getChat_username() {

    return chat_username;

    }

prefix 的值在 application.yml 中定义

@ConfigurationProperties(prefix = "mongo.config")public class MongoConfig {

    .....

整个完整代码如下:

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

/** * Created by Administrator on 2018/4/4. */@Component(value = "MongoConfig")@ConfigurationProperties(prefix = "mongo.config")public class MongoConfig {

 

    private static  String chat_username;

 

    private static  String chat_password ;

 

    private static  String chat_host;

 

    private static int chat_port;

 

    private static String chat_dbname;

 

    private static String chat_collprefix;

 

    public static String getChat_username() {

    return chat_username;

    }

 

    @Value("${mongo.config.username}")

    public void setChat_username(String chat_username) {

    MongoConfig.chat_username = chat_username;

    }

 

    public static String getChat_password() {

    return chat_password;

    }

 

    @Value("${mongo.config.password}")

    public void setChat_password(String chat_password) {

    MongoConfig.chat_password = chat_password;

    }

 

    public static String getChat_host() {

    return chat_host;

    }

 

    @Value("${mongo.config.host}")

    public void setChat_host(String chat_host) {

    MongoConfig.chat_host = chat_host;

    }

 

    public static int getChat_port() {

    return chat_port;

    }

 

    @Value("${mongo.config.port}")

    public static void setChat_port(int chat_port) {

    MongoConfig.chat_port = chat_port;

    }

 

    public static String getChat_dbname() {

    return chat_dbname;

    }

 

    @Value("${mongo.config.dbname}")

    public void setChat_dbname(String chat_dbname) {

    MongoConfig.chat_dbname = chat_dbname;

    }

 

    public static String getChat_collprefix() {

    return chat_collprefix;

    }

 

    @Value("${mongo.config.collprefix}")

    public void setChat_collprefix(String chat_collprefix) {

    MongoConfig.chat_collprefix = chat_collprefix;

    }

}

yml配置文件定义

通过 profile 指定不同环境下使用不同的配置。active 指定激活的环境,比如 dev 或者 prod

spring:  application:    name: textml  profiles:    active: dev

 

---spring:  profiles: dev, default,test

mongo:  config:      username: "xxx"      password: "xxx"      host: "127.0.0.2"      port: 10001      dbname: "xxx"      collprefix: "xxxx"

---spring:  profiles: prodmongo:  config:      username: "xxx"      password: "xxx"      host: "xxxx"      port: 10001      dbname: "xxxx"      collprefix: "xxx"

测试

由于使用了MongoDB自定义配置,故使用 @SpringBootApplication(exclude = MongoAutoConfiguration.class) 排除Spring-boot 中自带的MongoDB配置。

@SpringBootApplication(exclude = MongoAutoConfiguration.class)public class Application {

 

    public static void main(String[] args) {

    SpringApplication.run(Application.class, args);

    System.out.println("--config value--username:" + MongoConfig.getChat_username());

    }

}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程