2016-04-18 14 views
1

私はSpringに問題があります。注釈を使用してシステムプロパティをロードする必要があります。注釈を使用してSpringでSystemプロパティを読み込む方法は?

私はこのアプローチをしようとしている:

@Value("${mySystemProperty}") 
private String mySystemPropertyValue; 

をしかし、私はこの作るとき:

System.out.println("mySystemPropertyValue="+mySystemPropertyValue); 
System.out.println("system.mySystemProperty="+System.getProperty("mySystemProperty")); 

をそれが返されます。

mySystemPropertyValue=null 
system.mySystemProperty=myValue 

間違っているのですか?

EDIT

おかげで私はすべてしようとしているが、私はいつも見返りに、すべてのシステムプロパティのnull値を取得します。あなたのコンフィギュレーションクラスがいくつかBeanFactoryPostProcessor含まれているようだ

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true) 
public class SecurityContextConfig extends WebSecurityConfigurerAdapter 
{ 
    @Value("#{systemProperties['your.system.property']}") 
    private String property; 
    ... 
} 
+2

@Value


UPDATE
シンプルなアプリ実証使い方はこれを試してみてください。http://stackoverflow.com/questions/6285978/is-there-any-syntax-like- systemproperties-environment-variable-name-to-g – rapasoft

+0

'System.out'はどこに置かれますか? SpringのマネージドBeanまたは単に普通のものの '@ Value'もあります。 –

+0

私は@Value( "#{systemProperties ['mySystemProperty']}")と@Value( "#{systemProperties.mySystemProperty}")の両方を試しました。 –

答えて

2

のようなものを試してみてください...

@Autowired 
private Environment environment; 

しかし、 "環境" 変数がnullの場合:

は、私も試してみました。あなたがこのプロパティーを解決するためにこのBeanを必要とするので、私はそれがPropertySourcesPlaceholderConfigurerであると信じています。

春のjavadocからのいくつかの説明:@Autowired@Value("#{systemProperties['your.system.property']}")は、Configクラス内で動作しない理由である

Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static.

。あなたはそれ

@Bean 
public static PropertySourcesPlaceholderConfigurer pspc() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

を持っており、これ以上の非静的BFPPを返す@Bean方法がないことを確認していない場合


は、だからあなたの問題は、単に静的 PropertySourcesPlaceholderConfigurerを返すメソッドを作るか、またはそのような方法を追加し解決します。 BFPP Beanを移動して、 @Configurationクラスを分離することもできます。 @Configuration

@Configuration 
public class SimpleJavaConfig { 

    @Value("${java.version}") 
    private String property; 

    public static void main(String[] args) throws IOException { 
     ApplicationContext app = new AnnotationConfigApplicationContext(SimpleJavaConfig.class); 
     System.out.println("|" + app.getBean("propertyBean") + "|"); 
    } 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer pcc() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean 
    public String propertyBean() { 
     return property; 
    } 
} 
+0

それを試みたが、それは動作しません。 –

1

+0

あなたの答えをありがとう、しかし、私はどのようにBean PropertySourcesPlaceholderConfigurerを使用してプロパティを読み取ることができないのか分かりません。私にアイデアを教えてもらえますか?ありがとう –

+0

@AlessandroC 'PropertySourcesPlaceholderConfigurer'とは何も特別なことはありません。コンテキストに追加するだけでプロパティ値が解決されます。このBeanは '@Value(" $ {mySystemProperty} ")' 'を動作させるために必要です。 – Evgeny

+0

私はあなたの指示のようにBeanを追加しましたが、@ Value( "$ {my-property}")で変数を作成しようとすると、nullを返します...なぜか分かりません! –

関連する問題