springboot获取profile的操作

网友投稿 850 2022-12-10

springboot获取profile的操作

springboot获取profile的操作

目录springboot获取profile通过代码获取profile通过注解的方式来获取Profilespring profile的基本使用Spring profile在我们系统中的使用非常简单我们的问题出在哪里呢?

springboot获取profile

通过代码获取profile

@Component

public class ProfileUtils implements ApplicationContextAware {

private static ApplicationContext context = null;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.context = applicationContext;

}

public static String getActiveProfile(){

String[] profiles = context.getEnvironment().getActiveProfiles();

if(profileDEUnEmIRs.length != 0){

return profiles[0];

}

return "";

}

}

通过注解的方式来获取Profile

@Profile("dev","test")

//下面的配置信息只DEUnEmIR有在dev环境和test环境会生效

@Service

spring profile的基本使用

Spring profile是Spring 3引入的概念,一般系统开发的时候更喜欢使用Maven中的profile来进行不同环境配置文件的区分。Spring的profile一直没有怎么使用,最近在一个公司系统开发过程中同事使用了Spring profile。可是在设置default profile上遇到了麻烦。跟着一起研究了半天,才发现了问题所在。

Spring profile在我们系统中的使用非常简单

并没有使用runtime的特性,只是在xml中定义了不同profile环境中的beans

.......

location="classpath*:/application.properties" />

....

location="classpath*:/application.properties

,classpath*:/application.development.properties"/>

....

接下来我们一般可以通过在web.xml,在properties中来选择使用什么环境的profile,例如

spring.profiles.default

production

System.setProperty("spring.profiles.active", "development");

我们的问题出在哪里呢?

出在了我们想通过jndi而不是jvm参数来选择默认的profile,首先我们知道spring profile的设置不能在properties文件里,因为spring的加载顺序。我们不想改变系统的启动参数,所以选择了jndi的方式来读取profile的默认启动,关键来了,在配置jndi的时候我们进行了以下设置

悲哀的发现不起作用,spring的log里面也没有任何有用的提示,还没到获取profile相关的log就因为读取不到bean挂了。只好去掉了profile相关的xml文件重启启动一次系统才发现spring默认读取的jndi名字是spring.profiles.default而不是spring.profiles.active。

location="classpath*:/application.properties" />

....

location="classpath*:/application.properties

,classpath*:/application.development.properties"/>

....

接下来我们一般可以通过在web.xml,在properties中来选择使用什么环境的profile,例如

spring.profiles.default

production

System.setProperty("spring.profiles.active", "development");

我们的问题出在哪里呢?

出在了我们想通过jndi而不是jvm参数来选择默认的profile,首先我们知道spring profile的设置不能在properties文件里,因为spring的加载顺序。我们不想改变系统的启动参数,所以选择了jndi的方式来读取profile的默认启动,关键来了,在配置jndi的时候我们进行了以下设置

悲哀的发现不起作用,spring的log里面也没有任何有用的提示,还没到获取profile相关的log就因为读取不到bean挂了。只好去掉了profile相关的xml文件重启启动一次系统才发现spring默认读取的jndi名字是spring.profiles.default而不是spring.profiles.active。

location="classpath*:/application.properties

,classpath*:/application.development.properties"/>

....

接下来我们一般可以通过在web.xml,在properties中来选择使用什么环境的profile,例如

spring.profiles.default

production

System.setProperty("spring.profiles.active", "development");

我们的问题出在哪里呢?

出在了我们想通过jndi而不是jvm参数来选择默认的profile,首先我们知道spring profile的设置不能在properties文件里,因为spring的加载顺序。我们不想改变系统的启动参数,所以选择了jndi的方式来读取profile的默认启动,关键来了,在配置jndi的时候我们进行了以下设置

悲哀的发现不起作用,spring的log里面也没有任何有用的提示,还没到获取profile相关的log就因为读取不到bean挂了。只好去掉了profile相关的xml文件重启启动一次系统才发现spring默认读取的jndi名字是spring.profiles.default而不是spring.profiles.active。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:使用list stream: 任意对象List拼接字符串
下一篇:使用list stream:对List中的对象先进行排序再获取前n个对象
相关文章

 发表评论

暂时没有评论,来抢沙发吧~