简体   繁体   中英

How do I access a property via the ApplicationContext

When a properties file is used in a Spring ApplicationContext, its properties may be accessed in this manner: ${someproperty} inside your xml configuration files. But how do you access the same property within your java code without injecting it via xml?

ApplicationContext config

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="myapp.properties" />
    </bean>
    <bean class="my.app.MyClass">
        <property name="foo" value="${someproperty}" />
    </bean>
</beans>

Properties

someproperty=somevalue


The actual point of this, is a special case where a unique id for the application is set in the properties file (ez editing by sysadmin). Several of the application classes implement ApplicationContextAware so they have access to the context and to prevent injecting in each class or defining a bean for every class we want an ez property access method. Our app "knowing" about Spring is not an issue in this case.

It doesn't have any sense to access a property, it contradicts the IoC principle, the main aim of the Spring. In additional to other answers, may be you need all properties? In this case there is a PropertiesFactoryBean object which could give you Properties object to have access for all properties.

The PropertyPlaceholderConfigurer bean is designed to replace placeholders in a spring context. Any other usages are just confusing at least.

Good style has your code knowing as little about Spring as possible, but you can inject it via the @Value annotation (provided you're using Spring 3) which you put on the property or constructor (depending on how you build your bean). Otherwise, you can pick it out of the webapp's configuration, but in my experience that's considerably more error-prone. (The other good thing about doing it with Spring is that it makes it easy to merge properties from multiple different sources with complex override rules. Doing that manually is painful.)

I would fetch the placeHolderConfig bean (eg from the applicationContext or by injecting it), the PropertyPlaceholderConfigurer has an access method for this, have a look here:

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/PropertyResourceConfigurer.html#convertPropertyValue(java.lang.String )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM