2016-09-29 21 views
0

JavaコードからSpringプロパティにアクセスできない問題が発生しています。ファイルのプロパティがSpringで見つかりません

<context:property-placeholder ignore-resource-not-found="false" 
    location="file:/${setup.properties.directory}/setup.properties"/> 

setup.propertiesファイルは次のようになります:

paymentProvider.x.url=x 

コードは次のとおりです。

SpringContext.INSTANCE.getEnvironment() 
        .getProperty("paymentProvider.x.url"); 

は、実行時にエラーはありません。ここ

は文脈です。ただし、上のコードの結果は nullとなります。

誰でも知っていますか?

+0

pom.xmlを共有していただけますか?プロパティファイルがクラスパスにある場合は、リンクhttps://www.mkyong.com/spring/spring-propertysources-example/ – VelNaga

+0

に従ってください。 @VelNaga、4.3.1.RELEASE。 – ArthurDn

+0

@Valueアノテーションでプロパティを取得する方法を確認しましたか?このプロパティはクラスパスまたは静的パスにもありますか? – VelNaga

答えて

0

変更のようcontext:property-placeholder設定:あなたのようなそのクラスレベルの使用に1つの注釈をこのプロパティの値にアクセスしたい

<context:property-placeholder location="classpath:setup.properties" /> 

@PropertySource(value = { "classpath:setup.properties" }) 

とプロパティ値を読み取るためには、その内部でEnvironmentをautowireクラスのように:

@Autowired 
private Environment environment; 

そして最後に、あなたは次のようにアクセスすることができます。私のために

environment.getRequiredProperty("paymentProvider.x.url"); 
0

のみ

@Configuration 
@PropertySource("file:/${setup.properties.directory}/setup.properties") 
public class SpringContext { 
public static final ApplicationContext PROPERTIES_INSTANCE = new AnnotationConfigApplicationContext 
     (SpringContext.class); 
} 

作品。

関連する問題