19

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

私が春から見たいのは、現在アクティブなプロファイルとともに、使用中の春のバージョンを印刷するものです。

そのバージョンとアクティブなプロファイルを印刷するためにSpringを入手するにはどうすればよいですか?

4

3 に答える 3

24

EnvironmentAwareインターフェイスを実装する

例えば

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

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

このクラスをSpringBeanとしてマークします

また

熱心にロードしているBeanの1つに注入Environmentし、そこから必要な詳細を印刷するだけです。

お気に入り

@Autowired
Environment env;

熱心にロードしているBeanに入れて、印刷します

于 2012-12-30T08:32:20.227 に答える
12

Environmentオブジェクトはプロファイルのアクティブ化をレベルでログに記録するため、log4jを構成することで取得できDEBUGます。

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

A1ログアペンダーの場合。残念ながら、DEBUGレベルでは他にもたくさんのものがあるので、それはあまり良くありませんが、ソースを変更せずにアクティブなプロファイルを取得できます。

構成は、起動時にスタンドアロンのSwingアプリにログインします。

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

これはデバッグレベルのログに依存しているため、壊れやすいことに注意してください。このログは、Springへのコミットごとに急速に変化する可能性があります。

于 2012-12-30T09:55:03.467 に答える
10
@Value("${spring.profiles.active}")
private String activeProfiles;

これにより、アクティブなプロファイルを持つ文字列が得られます。

構成にDefaultFormattingConversionServiceを含める場合:

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

文字列のリストを返します。

@Value("${spring.profiles.active}")
private List<String> activeProfiles;
于 2017-05-18T10:32:30.043 に答える