简体   繁体   English

@Value注释和数据库PropertyPlaceHolderConfigurer

[英]@Value Annotation and a Database PropertyPlaceHolderConfigurer

I've got two PropertyPlaceHolderConfigurer in my Spring XML. 我的Spring XML中有两个PropertyPlaceHolderConfigurer。 The first one obtains application properties from a file. 第一个从文件获取应用程序属性。 The second one obtains user properties from database and looks like this: 第二个从数据库获取用户属性,如下所示:

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

It works properly and I can access to these properties injecting 'propertiesService' like: 它可以正常工作,我可以访问注入“ propertiesService”的这些属性,例如:

@Autowired
private PropertiesService propertiesService;

Which is: 这是:

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

The problem is that I like to use @Value annotation but it does not work. 问题是我喜欢使用@Value注释,但是它不起作用。

I've tried: 我试过了:

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

And, of course, the property exists and is reachable through 'propertiesService' but it results in the next error: 并且,当然,该属性存在并且可以通过“ 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)

Which makes me think that Spring is looking for a 'getHelloWorld()' method instead using 'getProperty(String key)' 这让我认为Spring正在寻找一种“ getHelloWorld()”方法,而不是使用“ getProperty(String key)”

Any suggestion? 有什么建议吗?

You need to explicitly specify which method is to be invoked, on bean referred in EL, and supply the argument value as below 您需要在EL中引用的bean上明确指定要调用的方法,并提供如下的参数值

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

@Value: It is used for expression-driven dependency injection. @Value:用于表达式驱动的依赖项注入。 If you want to use with below example. 如果要与下面的示例一起使用。

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

..
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有PropertyPlaceholderConfigurer bean的Spring @Configuration文件无法解析@Value注释 - Spring @Configuration file with PropertyPlaceholderConfigurer bean doesn't resolve @Value annotation Spring多个PropertyPlaceholderConfigurer文件和数据库 - Spring Multiple PropertyPlaceholderConfigurer files and database PropertyPlaceholderConfigurer,用于读取属性文件和数据库 - PropertyPlaceholderConfigurer for reading property files and database Spring PropertyPlaceholderConfigurer注入了错误的值 - Wrong value injected with Spring PropertyPlaceholderConfigurer REQ:检索我的Java应用程序中的属性。 由“ PropertyPlaceholderConfigurer”收集—属性作为键/值存储在数据库中 - REQ: Retrieving properties in my java app. collected by “PropertyPlaceholderConfigurer” — properties are stored in a database as key/value PropertyPlaceholderConfigurer扩展后,无法对扩展类使用批注 - I can't use annotation to the extending class when PropertyPlaceholderConfigurer was extended PropertyPlaceholderConfigurer:我可以拥有动态位置值 - PropertyPlaceholderConfigurer: can i have a dynamic location value Spring PropertyPlaceholderConfigurer默认值覆盖实际属性值 - Spring PropertyPlaceholderConfigurer Default Value overwriting actual property value PropertyPlaceholderConfigurer动态位置值:系统找不到指定的路径 - PropertyPlaceholderConfigurer dynamic location value : The system cannot find the path specified Spring 3.5 设置 xml 属性,使用 PropertyPlaceholderConfigurer 处理默认值 - Spring 3.5 Setting an xml property, handling the default value using PropertyPlaceholderConfigurer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM