java 如何让 Spring 打印出哪些弹簧配置文件处于活动状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14089301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:01:33  来源:igfitidea点击:

How to get Spring to print out what spring profiles are active?

javaspring

提问by ams

I am using spring 3.1 profiles and want would like Spring to print out on startup what profiles are active. For example are the first few lines of the output in the log file.

我正在使用 spring 3.1 配置文件,并希望 Spring 在启动时打印出哪些配置文件处于活动状态。例如是日志文件中输出的前几行。

02:59:43,451 INFO  [ContextLoader] Root WebApplicationContext: initialization started
02:59:43,544 INFO  [XmlWebApplicationContext] Refreshing Root WebApplicationContext: startup date [Sun Dec 30 02:59:43 EST 2012]; root of context hierarchy
02:59:43,610 INFO  [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring.xml]
02:59:43,835 INFO  [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [spring-security.xml]
02:59:43,971 INFO  [SpringSecurityCoreVersion] You are running with Spring Security Core 3.1.3.RELEASE
02:59:43,971 INFO  [SecurityNamespaceHandler] Spring Security 'config' module version is 3.1.3.RELEASE

What I want to see from spring is something that prints out the version of spring in use along with which profiles are currently active.

我想从 spring 中看到的是打印出正在使用的 spring 版本以及当前活动的配置文件的东西。

How can I get spring to printout it's version and what profiles are active?

我怎样才能让 spring 打印出它的版本以及哪些配置文件处于活动状态?

回答by Jigar Joshi

Implement EnvironmentAwareinterface

实现EnvironmentAware接口

For example

例如

class MyEnvironmentAware implements EnvironmentAware{
    private static Environment env = null;

    @Override
    public void setEnvironment(Environment environment) {
            env = environment;
            //log the stuff you want here
     }
}

Mark this class as Spring bean

将这个类标记为 Spring bean

or

或者

just inject Environmentin one of your eagerly loading bean and print the detail you need from it

只需注入Environment您急切加载的 bean 之一并从中打印您需要的详细信息

like

喜欢

@Autowired
Environment env;

in your eagerly loading bean, and print it

在你急切地加载 bean 中,并打印它

回答by burna

You could get it by configuring log4j, as the Environmentobject logs the activating of the profile at DEBUGlevel.

您可以通过配置 log4j 来获取它,因为该Environment对象会在DEBUG级别记录配置文件的激活。

log4j.logger.org.springframework.core.env=DEBUG, A1

If A1is your log appender. Unfortunately, there is a lot of other stuff coming at DEBUG level, so it is not really nice, but you get the active profile without source modification.

如果A1是您的日志附加程序。不幸的是,在调试级别还有很多其他的东西,所以它不是很好,但是您无需修改​​源代码即可获得活动配置文件。

The config logs in a standalone Swing app on startup:

该配置在启动时登录一个独立的 Swing 应用程序:

58   [main] DEBUG org.springframework.core.env.StandardEnvironment  - Activating profile 'production'

Note, this is fragile, as it relies on debug level logs, which can change rapidly in every commit to spring.

请注意,这是脆弱的,因为它依赖于调试级别的日志,在每次提交到 spring 时都会快速更改。

回答by ?ukasz Chor??y

@Value("${spring.profiles.active}")
private String activeProfiles;

This will give you a string with active profiles.

这将为您提供一个带有活动配置文件的字符串。

If you include in your configuration DefaultFormattingConversionService:

如果您在配置中包含 DefaultFormattingConversionService:

@Bean
public ConversionService conversionService() {
    return new DefaultFormattingConversionService();
}

it will return a List of strings:

它将返回一个字符串列表:

@Value("${spring.profiles.active}")
private List<String> activeProfiles;