简体   繁体   English

如何从bean的方法访问Spring托管属性?

[英]How to access Spring managed properties from bean's methods?

I have list of properties mantianed in .properties file. 我有.properties文件中的属性列表。 Now I am managing those property file wiht PropertyPlaceholderConfigurer . 现在,我正在使用PropertyPlaceholderConfigurer管理这些属性文件。

I want to access property value from one of the method. 我想从其中一个方法访问属性值。 Can anyone please suggest how to achieve that? 任何人都可以建议如何实现这一目标?

example

connection.properties
dev.url = "http://localhost:8080/"
uat.url = "http://xyz.com"

Now I have configured `PropertyPlaceholderConfigurer bean by specifing connection.properties 现在我通过指定connection.properties配置了`PropertyPlaceholderConfigurer bean

I have one method which reads url based on mode of deployment so base on mode of deployment I want to change url using property file. 我有一个基于部署模式读取URL的方法,所以基于部署模式,我想使用属性文件更改URL。

Please let me know if this is right approach. 如果这是正确的方法,请告诉我。

If you have any suggestion please give. 如果您有任何建议请给。

The PropertyPlaceholderConfigurer does not expose its properties . PropertyPlaceholderConfigurer 不公开其属性。 You could however easily read in the properties file anew using eg PropertiesLoadUtils : 但是,您可以使用例如PropertiesLoadUtils轻松读取属性文件:

PropertiesLoaderUtils.loadProperties(
        new ClassPathResource("/connection.properties"));

也许你正在寻找类似@Value注释的东西?

private @Value("#{connection.dev.url}") String myURL;

Not clear what you are looking for, but I have utility where it loads properties based on environment its running on (dev,prod) 不清楚你在寻找什么,但我有实用程序,它根据环境运行(dev,prod)加载属性

public class EnvironmentalPlaceHolderConfigurer extends PropertyPlaceholderConfigurer
        implements InitializingBean {

    private Resource overrideLocation;

    public void setOverrideLocation(Resource overrideLocation) {
        this.overrideLocation = overrideLocation;
    }
    if(overrideLocation != null){
            if( overrideLocation.exists())
                super.setLocation(overrideLocation);
            else{
                logger.warn("Unbale to find "+overrideLocation.getFilename() +" using default");
            }
        }else{
            logger.warn("Override location not set, using default settings");
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        setProperLocation();
    }

Then you need to define bean as 然后你需要将bean定义为

<bean class="com.commons.config.EnvironmentalPlaceHolderConfigurer">
    <property name="overrideLocation" value="classpath:/jms/${ENV_NAME}-jms.properties" />
    <property name="location" value="classpath:/jms/jms.properties" />
</bean>

You need to define a environment entry on machine with key as "ENV_NAME" 您需要在计算机上定义一个环境条目,其密钥为“ENV_NAME”

Ex: ENV_NAME=prod 例如:ENV_NAME = prod

In case of windows environment variable and in case of unix entry in .profile. 在windows环境变量的情况下以及.profile中的unix条目的情况下。

You need to maintain properties for each environment in following fashion prod-jms.properties uat-jms.properties 您需要按照以下方式维护每个环境的属性prod-jms.properties uat-jms.properties

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

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