2017-06-20 2 views
0

私は、春のbeanで値がプロパティファイルから注入されているアプリケーションを持っています。 constants.javaファイルから値を挿入します。 どのような変更が必要ですか?定数からの豆の注入値javaファイル

のSpring Bean

@Value("${resetPassword.email.key}") 
    private String resetPassword; 

プロパティConstants.java

resetPassword.email.key = RESET_PASSWORD 

ファイル

public static final String resetPassword_email_key = "RESET_PASSWORD"; 

答えて

0

あなたは、静的プロパティに値を挿入することはできません。しかし、あなたは以下のように非静的セッターに割り当てることができます。

@Component 
public class GlobalValue { 

    public static String DATABASE; 

    @Value("${mongodb.db}") 
    public void setDatabase(String db) { 
     DATABASE = db; 
    } 

} 

+0

私のspring beanの 'resetPassword'プロパティは静的ではありません。 –

+0

@shivakumar:これを意味しますか? 'public static final String resetPassword_email_key =" RESET_PASSWORD ";' – htpvl

+1

@htpvlいいえ、私はOPが 'private String resetPassword; 'を意味すると思います。 – naXa

4

https://www.mkyong.com/spring/spring-inject-a-value-into-static-variables/からあなたはプロパティファイルでJavaの定数を参照することはできません。そして、あなたはそれのためにスプリングインジェクションを必要としません。あなたは複数のサブプロジェクト(プロジェクトのモジュール)との間にConstantsクラスを共有する必要がある場合あなたは、単に、あなたが他のプロジェクトに含まれることができるライブラリにこのクラスを抽出するために

private String resetPassword = Constants.resetPassword_email_key; 

たいことがあります。

+0

私のアプリケーションでは春が使用されており、削除できません。 –

+0

@shivakumarそれを削除しないでください。私はあなたがこの特定の問題を解決するためにSpringを必要としないと言っています。 – naXa

0

@Valueで値を注入するには、プロパティーファイルかspring beanを使用します。プロパティはutil:propertiesタグでそれを宣言ファイルを使用するには

<util:properties id="jdbcProperties" location="classpath:org/example/config/jdbc.properties"/> 

と春の豆では、実行します。

private @Value("#{jdbcProperties.url}") String jdbcUrl; 
    private @Value("#{jdbcProperties.username}") String username; 
    private @Value("#{jdbcProperties.password}") String password; 

それとも、のような他のSpring Beanの値を挿入することができます

@Value("#{strategyBean.databaseKeyGenerator}") 
public void setKeyGenerator(KeyGenerator kg) { … } 

詳細情報:http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html

関連する問題