3

我有一个属性文件,我想在其中修改某些属性以包含用户 ID(可能还有其他运行时加载的值),如下所示...

emailer.queuename=Emailer.${user}

...所以在这个例子中,Emailer.SMITHJ读入时属性值可能会变成。

我想过扩展Properties类并覆盖getProperty method,但我还需要确保 Spring 在PropertyPlaceholderConfigurer用于上下文时会相应地修改属性(我看到我可以convertPropertyValue在这个类中覆盖)。

但是有没有一种巧妙的方法可以实现两者?

谢谢

====================================

更新:

在我的案例中,我发现的简单解决方案就是使用系统属性(例如 ${user.name}),并且systemPropertiesMode因为PropertyPlaceholderConfigurer类设置为fallbackSpring 会自动对其进行排序。但是,对于直接使用属性文件的情况,我仍然需要创建一个继承类,或者只是将值注入到使用它的类中。

4

1 回答 1

0

If I understand you correctly, you wish to replace some properties in property files before they are used to resolve properties in bean configurations.

As you have said, bean properties are resolved by PropertyPlaceholderConfigurer which is a BeanFactoryPostProcessor. To perform upfront processing you will also need a BeanFactoryPostProcessor and implement the Ordered interface to specify when it will run.

Alternatively if you register your BeanFactoryPostProcessors programatically, they will be executed in the order registered and before any other bean's registered through other means.

于 2013-11-04T12:49:09.133 回答