2016-11-08 11 views
0

Spring XMLにPropertyPlaceHolderConfigurerが2つあります。最初のファイルは、ファイルからアプリケーションのプロパティを取得します。もう一つは、データベースからユーザープロパティを取得し、次のようになります。@Value注釈とデータベースPropertyPlaceHolderConfigurer

<myConfiguration> 
<bean id="databaseProperties" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
      <property name="properties"> 

       <bean class="org.apache.commons.configuration.ConfigurationConverter" 
        factory-method="getProperties"> 
        <constructor-arg> 
         <ref bean="propertiesSource" /> 
        </constructor-arg> 
       </bean> 
      </property> 
     </bean> 

     <bean id="propertiesSource" class="org.apache.commons.configuration.DatabaseConfiguration"> 
      <constructor-arg type="javax.sql.DataSource" ref="tomcatDataSource" /> 
      <constructor-arg value="application_properties" /> 
      <constructor-arg value="PROPERTY_KEY" /> 
      <constructor-arg value="PROPERTY_VALUE" /> 
     </bean> 

     <bean id="propertiesService" class="com.xxx.PropertiesServiceImpl"> 
      <property name="propertiesSource" ref="propertiesSource"></property> 
     </bean> 
<myConfiguration> 

それが正常に動作し、私はLIKE「propertiesService」を注入し、これらのプロパティにアクセスすることができます

public class PropertiesServiceImpl implements PropertiesService { 

    @Autowired 
    private DatabaseConfiguration propertiesSource; 


    private Properties properties; 

    @Override 
    public String getProperty(String key) { 
     if (properties == null) { 
      properties = ConfigurationConverter.getProperties(propertiesSource); 
     } 
     return properties.getProperty(key); 
    } 


    @Override 
    public Properties getProperties() { 
     if (properties == null) { 
      properties = ConfigurationConverter.getProperties(propertiesSource); 
     } 
     return properties; 
    } 
:ある

@Autowired 
private PropertiesService propertiesService; 

@Valueアノテーションを使用するのが問題ですが、機能しません。

私が試した:

private @Value("#{propertiesService.helloWorld}") String helloWorld; 

をそして、もちろん、プロパティが存在し、「propertiesService」を介して到達可能であるが、それは次のエラーが発生:

私は考えさせる
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 18): Property or field 'helloWorld' cannot be found on object of type 'com.infraportal.model.properties.PropertiesServiceImpl' - maybe not public? 
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:224) 
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) 
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:46) 
    at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:374) 
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) 
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) 
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242) 
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161) 

そのSpringは 'getProperty(String key)'を使用する代わりに 'getHelloWorld()'メソッドを探しています

何か提案がありますか?

+1

以下のように引数の値を指定する必要があり

sample code

@Configuration @PropertySource("classpath:jdbc.properties") public class AppConfig { @Value("${jdbc.driverClassName}") private String driverClassName; @Value("${jdbc.url}") private String jdbcURL; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; ..... .. } 

+0

こんにちは。私は投稿を更新しました。 – cape

+2

Spring> = 3.1 PropertyPlaceholderConfigurerを介して 'PropertySourcesPlaceholderConfigurer'を推奨http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html – Ralph

答えて

0

@Value:これは、発現駆動依存性注入に使用されます。 以下の例で使用したい場合。あなたが明示的にELに言及Beanで、呼び出されるべきメソッドを指定し、そしてどのように@valueを使用している

@Value("#{propertiesService.getProperty('helloWorld')}") 
private String helloWorld; 
1

?何を試しましたか?