SpringBoot中获取profile的方法详解

目录
  • spring boot与profile
    • 静态获取方式
    • autowire ProfileConfig

spring boot与profile

spring boot 的项目中不再使用xml的方式进行配置,并且,它还遵循着约定大于配置。

静态获取方式

静态工具类获取当前项目的profile环境。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * 

 */
/**
 * @author wangjiuzhou (835540436@qq.com)
 * @date 2018/10/27
 * 项目名称:
 * 类名: SpringContextUtil
 * 描述: 获取bean的工具类,可用于在线程里面获取bean
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    public static final String LOCAL_PROFILE = "local";
    public static final String DEV_PROFILE = "dev";
    public static final String TEST_PROFILE = "test";
    public static final String PRO_PROFILE = "pro";

    private static ApplicationContext context = null;

    /* (non Javadoc)
     * @Title: setApplicationContext
     * @Description: spring获取bean工具类
     * @param applicationContext
     * @throws BeansException
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }

    // 传入线程中
    public static <T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }

    // 国际化使用
    public static String getMessage(String key) {
        return context.getMessage(key, null, Locale.getDefault());
    }

    // 获取当前环境
    public static String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }
}

点评:

这种方式在使用起来很方便也是现在各个博客文章所撰写的方式,在很多Service的业务代码中使用起来很方便,毕竟是静态的方式嘛!

但是有一种缺陷,因为实现ApplicationContextAware接口,而spring中的这个接口是在所有的Bean注入完毕,才会执行setApplicationContext方法,那么问题来了,往往在项目中我们可能会对一些Bean进行一些config操作,例如:@Bean注入,而有时候我们会根据不同的profile进行不同的定制化config。这个时候恰恰我们的工具类SpringContextUtil还没有执行setApplicationContext此时工具类中的context对象还是null。就会出现异常的情况。下面的方式可以弥补这个缺陷。

autowire ProfileConfig

使用这种方式首先声明一下,其实就相当于一个特殊的configBean一样,因为只有这样,这个类才不会在所有bean全部加载完毕后才能获取到context。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangjiuzhou (835540436@qq.com)
 * @date 2018/11/07
 *
 * 获取当前项目环境:local、dev、test、pro
 */
@Configuration
public class ProfileConfig {
    public static final String LOCAL_PROFILE = "local";
    public static final String DEV_PROFILE = "dev";
    public static final String TEST_PROFILE = "test";
    public static final String PRO_PROFILE = "pro";

    @Autowired
    private ApplicationContext context;

    public String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }
}

点评:

ProfileConfig ,首先是作为一个相当于Bean的形式存在着,此处的不在解释@configuration和@component的区别;

注入ApplicationContext因为该接口extends于EnvironmentCapable,所以可以获取到环境的一些信息;

以上就是SpringBoot中获取profile的方法详解的详细内容,更多关于SpringBoot获取profile的资料请关注我们其它相关文章!

(0)

相关推荐

  • springboot获取profile的操作

    目录 springboot获取profile 通过代码获取profile 通过注解的方式来获取Profile spring profile的基本使用 Spring profile在我们系统中的使用非常简单 我们的问题出在哪里呢? springboot获取profile 通过代码获取profile @Component public class ProfileUtils implements ApplicationContextAware { private static ApplicationC

  • SpringBoot 多Profile使用与切换方式

    Spring中Profile对不同环境提供不同配置功能的支持,可以通过激活.指定参数等方式快速切换环境. 文件名格式:application-{profile}.properties 可以建立多个properties(yaml)文件来不断的切换 application-dev.properties server.port=8082 application-prod.properties server.port=8083 application.properties server.port=808

  • springboot入门 之profile设置方式

    目录 配置方式 命令行方式 系统变量方式 Java系统属性方式 配置文件方式 优先级 激活多个profile 配置方式 命令行方式 命令行方式是一种外部配置的方式,在执行java -jar命令时可以通过 --spring.profiles.active=test的方式进行激活指定的profiles列表. 使用方式如下所示: java -jar order-service-v1.0.jar --spring.profiles.active=dev 系统变量方式 需要添加一个名为SPRING_PRO

  • SpringBoot 动态配置Profile环境的方式

    下面的例子是通过修改开发环境和生产环境的动态配置的端口号的示例: 开发环境端口号是 8081 生产环境端口号是 8082 springboot的配置方式 springboot的配置有两种:properties和yaml或yml properties方式配置 1.在application.properties配置环境 创建两个application-xx.properties 一个application-dev.properties #开发环境 一个application-pro.properti

  • SpringBoot激活profiles的几种方式

    多环境是最常见的配置隔离方式之一,可以根据不同的运行环境提供不同的配置信息来应对不同的业务场景,在SpringBoot内支持了多种配置隔离的方式,可以激活单个或者多个配置文件. 激活Profiles的方式 激活的profiles要在项目内创建对应的配置文件,格式为application-{profile}.yml. 命令行方式 命令行方式是一种外部配置的方式,在执行java -jar命令时可以通过--spring.profiles.active=test的方式进行激活指定的profiles列表.

  • SpringBoot中获取profile的方法详解

    目录 spring boot与profile 静态获取方式 autowire ProfileConfig spring boot与profile spring boot 的项目中不再使用xml的方式进行配置,并且,它还遵循着约定大于配置. 静态获取方式 静态工具类获取当前项目的profile环境. import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte

  • SpringBoot中使用监听器的方法详解

    目录 1.监听器 2.SpringBoot中监听器的使用 2.1监听Servlet上下文对象 2.2监听HTTP会话Session对象 总结 1.监听器 web监听器是一张Servlet中特殊的类,它们能帮助开发者监听web中特定的事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁:变量的创建.销毁.和修改等.可以在某些动作前后增加处理,实现监控 2.SpringBoot中监听器的使用 web监听器的使用场景很多,比如监听servlet上下文

  • SpringBoot实现整合微信支付方法详解

    目录 1.准备工作 1.1 数据库表 1.2 实体类 1.3 导入依赖 1.4 配置文件 1.5 创建读取微信支付相关信息的工具类 1.6 其他工具类 2.生成订单 2.1 远程调用用户模块和课程模块 2.2 远程调用方法的实现 2.3 根据课程id和用户id生成订单 3.查询订单信息 3.1 controller层 3.2 service层 4.生成微信支付的二维码 4.1 controller层 4.2 service层 5.查询订单支付状态 5.1 controller层 5.2 serv

  • SpringBoot实现自定义事件的方法详解

    目录 简介 步骤1:自定义事件 步骤2:自定义监听器 方案1:ApplicationListener 方案2:SmartApplicationListener 步骤3:注册监听器 法1:@Component(适用于所有监听器) 法2:application.yml中添加配置 法3:启动类中注册 步骤4:发布事件 法1:注入ApplicationContext,调用其publishEvent方法 法2:启动类中发布 简介 说明 本文用实例来介绍如何在SpringBoot中自定义事件来使用观察者模式

  • SpringBoot导入导出数据实现方法详解

    今天给大家带来的是一个 SpringBoot导入导出数据 首先我们先创建项目 注意:创建SpringBoot项目时一定要联网不然会报错 项目创建好后我们首先对 application.yml 进行编译 server:  port: 8081# mysqlspring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://127.0.0.1:3306/dvd?characterEncodi

  • python编程之requests在网络请求中添加cookies参数方法详解

    哎,好久没有学习爬虫了,现在想要重新拾起来.发现之前学习爬虫有些粗糙,竟然连requests中添加cookies都没有掌握,惭愧.废话不宜多,直接上内容. 我们平时使用requests获取网络内容很简单,几行代码搞定了,例如: import requests res=requests.get("https://cloud.flyme.cn/browser/index.jsp") print res.content 你没有看错,真的只有三行代码.但是简单归简单,问题还是不少的. 首先,这

  • Android 中RxPermissions 的使用方法详解

    Android 中RxPermissions 的使用方法详解 以请求拍照.读取位置权限为例 module的build.gradle: compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar' compile 'io.reactivex.rxjava2:rxjava:2.0.5' AndroidManifest.xml: <uses-permission android:name="android.permission.AC

  • Android 中Context的使用方法详解

    Android 中Context的使用方法详解 概要: Context字面意思是上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄.很多方法需要通过 Context才能识别调用者的实例:比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用t

  • Vue 中使用 typescript的方法详解

    什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类型版本,除此之外,它还有一些扩展的语法,如interface/module等. typescript 在编译期会去掉类型和特有语法,生成纯粹的JavaScript. Typescript 5年内的热度随时间变化的趋势,整体呈现一个上升的趋势.也说明ts越来越️受大家的关注了. 安装typescript npm install -g ty

  • springboot命令行启动的方法详解

    springboot命令行启动 创建的springboot项目想看看效果,不想打开idea等开发工具,使用直接使用命令行启动. maven的命令启动 需要将 jdk的bin目录和maven的bin目录添加到环境变量path中,若是没有,mvn就要用在maven的bin环境中的全路径 若是没有添加环境变量 mvn就要是E:\software\apache-maven-3.3.9\bin\mvn(安装路径\bin\mvn) java就要是C:\software\jdk\bin\java.exe(安装

随机推荐