Announcement Announcement Module
No announcement yet.
custom PropertySource for placeholders Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • custom PropertySource for placeholders

    I have a need for my own property source (it gets its properties from a database rather than a file).
    I've extended PropertySource and I am creating it in the XML config file.

    <bean id="foobarPropertySource" class="com.example.FooBarPropertySource">
    <constructor-arg name="name" value="foobarPS" />
    <constructor-arg name="source" ref="foobar" />
    <bean class=" ySourcesPlaceholderConfigurer">
    <property name="propertySources" value="#{foobarPropertySource.propertySources}" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />

    But it's not working how I want it to work. Ideally I'd like it to try and resolve using my
    source then fall back to using system properties and environment if it doesn't
    find a match. In this config it uses foobarPropertySource but does not fall back
    to using the others. #{foobarPropertySource.propertySources} returns a
    MutablePropertySources object that just contains my PropertySource.

    I'd also like it to work for both <import /> and ordinary properties. I tried implementing
    ApplicationContextInitializer<ConfigurableWebAppli cationContext> and adding that to web.xml.
    I got that to work for <import /> but then it wasn't working for placeholders in ordinary properties.
    What am I missing?

  • #2
    I'll answer my own question

    I created this class

    public class SpringContextInitializer implements
            ApplicationContextInitializer<ConfigurableWebApplicationContext> {
        public void initialize(ConfigurableWebApplicationContext applicationContext) {
            MyPropertySource ps = new MyPropertySource();
    You can do addLast() instead if you prefer system and env to take precedence.

    in web.xml I added
    in my beans declaration file add
    <context:property-placeholder ignore-unresolvable="true" />
    The ApplicationContextInitializer is what is necessary to have this work for <import /> references. If you don't need that and your properties are in a standard properties file you don't need to do all this, you'll find easier examples elsewhere. If you leave out the last step this will only work for <import /> and all other placeholders will be ignored.
    ignore-unresolvable="true" prevents errors when you can't find the placeholder (which may or may not be what you want).