PropertyPlaceholderConfigurer获取属性文件指定key的value值

        今天花了一天的时间,研究了PropertyPlaceholderConfigurer这个类,首先它的作用是一个资源属性的配置器,能够将BeanFactory的里定义的内容放在一个以.propertis后缀的文件中。那么如何在Java类中获取指定key的value值呢!

步骤:

   一:首先创建一个属性文件

   二:然后自定义一个属性加载类工具类,必须要extends PropertyPlaceholderConfigurer类,(实际是对PropertyPlaceholderConfigurer类的一个扩展)

   public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {

    private static Map<String, Object> ctxPropertiesMap;

    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        ctxPropertiesMap = new HashMap<String, Object>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
    }

    public static Object getContextProperty(String name) {
        return ctxPropertiesMap.get(name);
    }
    
    public static Object setContextProperty(String name,Object value) {
        return ctxPropertiesMap.put(name, value);
    }

}
  三:然后再spring文件中配置如下

 <bean id="propertyConfigurer"
        class="com.ljzforum.platform.util.CustomizedPropertyConfigurer">
        <property name="locations">
            <list>
                <value>classpath:context/lxh.properties</value>
            </list>
        </property>
    </bean>   
 四:最后就是怎么使用了,代码如下

        String appId = (String) CustomizedPropertyConfigurer.getContextProperty("app_id");
        String app_secret =(String) CustomizedPropertyConfigurer.getContextProperty("app_secret");

很容易吧,如果有什么不懂的问题欢迎大家一起交流学习,本人扣扣2739677514,如有错误,也希望大家指出,谢谢



Spring Boot中,可以通过继承PropertyPlaceholderConfigurer类来获取配置文件中的配置项。 首先,创建一个类,继承自PropertyPlaceholderConfigurer,重写processProperties方法,该方法在Spring应用程序上下文启动时会被调用,可以在该方法中获取配置文件中的属性,例如: ```java public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private Properties properties; public MyPropertyPlaceholderConfigurer() { this.properties = new Properties(); } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.properties.putAll(props); } public String getProperty(String key) { return this.properties.getProperty(key); } } ``` 在该类中,我们重写了processProperties方法,并在该方法中将配置文件中的属性保存到一个Properties对象中。然后,提供了一个getProperty方法,可以根据属性获取属性。 接下来,在Spring Boot启动类中,注入该类的实例,并通过该实例获取配置文件中的属性,例如: ```java @SpringBootApplication public class MyApp { @Autowired private MyPropertyPlaceholderConfigurer propertyConfigurer; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @PostConstruct public void init() { String propertyValue = propertyConfigurer.getProperty("my.property"); System.out.println("my.property value is: " + propertyValue); } } ``` 在上述代码中,我们通过@Autowired注入了MyPropertyPlaceholderConfigurer实例,并在init方法中,获取了配置文件中的属性,并打印出来。 在application.yml或application.properties中,可以定义my.property属性,例如: ```yaml my.property: my custom property value ``` 这样,当应用启动时,就会打印出"my.property value is: my custom property value"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值