cape
cape

Reputation: 434

@Value Annotation and a Database PropertyPlaceHolderConfigurer

I've got two PropertyPlaceHolderConfigurer in my Spring XML. 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:

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

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:

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

Any suggestion?

Upvotes: 0

Views: 1676

Answers (2)

Ammar
Ammar

Reputation: 4024

You need to explicitly specify which method is to be invoked, on bean referred in EL, and supply the argument value as below

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

Upvotes: 1

Maheshwar Ligade
Maheshwar Ligade

Reputation: 6855

@Value: It is used for expression-driven dependency injection. 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;
.....

..
}

Upvotes: 0

Related Questions