spring项目输出当前的activeprofile

springboot spring 文章 2023-05-04 21:08 1445 0 全屏看文

AI助手支持GPT4.0

首先,在你的配置文件中,你需要指定当前的active profile。例如:

spring.profiles.active=dev

这里我将active profile设置为dev,你可以根据实际情况进行修改。


然后,在你的代码中,你可以通过注入Environment对象来获取当前的active profile。例如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@SpringBootApplication
@Configuration
public class MyApp {
    
    @Autowired
    private Environment environment;
    
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
    
    public void printActiveProfile() {
        String[] activeProfiles = environment.getActiveProfiles();
        if (activeProfiles.length > 0) {
            System.out.println("当前的active profile是:" + activeProfiles[0]);
        } else {
            System.out.println("没有设置active profile");
        }
    }
}

在上面的代码中,我们首先通过@SpringBootApplication注解来标记我们的应用程序,然后通过@Configuration注解来标记一个配置类。在这个配置类中,我们注入了Environment对象,并在printActiveProfile()方法中使用getActiveProfiles()方法来获取当前的active profile,并输出到控制台中。


最后,在main()方法中,我们通过SpringApplication.run()方法来启动我们的应用程序,并可以在需要的地方调用printActiveProfile()方法来输出当前的active profile。


-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
详细说说 Springboot ApplicationEvent 事件发布
spring使用注解编写拦截器
spring boot 调用 redis的两种方法
DataSource: 'url' attribute is not specified and no embedded datasource
spring里使用静态方法获取HttpServeltRequest
随便看看
企业微信通讯录账号被管理员误/恶意删除,怎么办? 9389
问题? 8909
如何快速搭建抽奖助手小程序(无需代码知识) 6039
许涛 大哥在吗, 要解冻小程序的时候提示信息主体不一致, 能帮忙看下吗? 7343
小程序搜一搜全称搜索不显示 麻烦解决一下!谢谢 6671
我的小程序广告收款主体是个体工商户,每个月邮寄两次发票很是麻烦,我年底的时候打包邮寄一次可以么? 5998
如何解决渲染层网络错误Failed to load media? 17274
小程序找号找回 释放昵称? 3149
我的小程序广告主收入5301,实际进账4406,是怎么回事呀? 3012
删除部分微信用户个人数据的通知,怎么删除? 6259