如何让Spring打印出哪些弹簧轮廓是活动的?

ams*_*ams 17 java spring

我正在使用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
Run Code Online (Sandbox Code Playgroud)

我想从春天看到的东西是打印出正在使用的弹簧版本以及当前活动的配置文件.

如何让弹簧打印出它的版本以及哪些配置文件是活动的?

Jig*_*shi 21

实现EnvironmentAware界面

例如

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

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

将此类标记为Spring bean

要么

只需注入Environment一个急切的加载bean并从中打印出您需要的细节

喜欢

@Autowired
Environment env;
Run Code Online (Sandbox Code Playgroud)

在你急切的加载豆子,打印它


bur*_*rna 11

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

log4j.logger.org.springframework.core.env=DEBUG, A1
Run Code Online (Sandbox Code Playgroud)

如果A1是你的日志追加者.不幸的是,在DEBUG级别还有很多其他的东西,所以它不是很好,但是你得到了没有源代码修改的活动配置文件.

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

58   [main] DEBUG org.springframework.core.env.StandardEnvironment  - Activating profile 'production'
Run Code Online (Sandbox Code Playgroud)

注意,这是脆弱的,因为它依赖于调试级别日志,它可以在每次提交到春季时快速变化.


Łuk*_*ąży 5

@Value("${spring.profiles.active}")
private String activeProfiles;
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个具有有效配置文件的字符串。

如果您在配置中包括DefaultFormattingConversionService:

@Bean
public ConversionService conversionService() {
    return new DefaultFormattingConversionService();
}
Run Code Online (Sandbox Code Playgroud)

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

@Value("${spring.profiles.active}")
private List<String> activeProfiles;
Run Code Online (Sandbox Code Playgroud)