2016-11-08 73 views
0

我的Spring XML中有两个PropertyPlaceHolderConfigurer。第一个从文件获取应用程序属性。第二个从数据库中获取用户的属性,看起来像这样:@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> 

它可以正常工作,我可以访问这些属性注入“propertiesService”,如:

@Autowired 
private PropertiesService 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; 
    } 

问题是我喜欢使用@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正在寻找'getHelloWorld()'方法,而不是使用'getProperty(String key)'

任何建议?

+1

提供参数值?你尝试了什么? –

+0

嗨。我已更新该帖子。 – cape

+2

Spring> = 3.1推荐'PropertySourcesPlaceholderConfigurer'通过'PropertyPlaceholderConfigurer' http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html – Ralph

回答

0

@Value:它用于表达式驱动的依赖注入。 如果你想使用下面的例子。

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; 
..... 

.. 
} 
1

你需要明确指定哪些方法是被调用,在EL简称豆,和你如何使用@Value如下

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