Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I came up with the following which seems to work, but I'm fairly new to Spring, so I'm not so sure how it will hold up under different use cases.</p> <p>Basically, the approach is to extend <code>PropertySourcesPlaceholderConfigurer</code> and add a setter to allow the user to easily configure a List of <code>PropertySource</code> objects in XML. After creation, the property sources are copied to the current <code>Environment</code>.</p> <p>This basically allows the property sources to be configured in one place, but used by both placholder configuration and Environment.getProperty scenarios.</p> <p><strong>Extended <code>PropertySourcesPlaceholderConfigurer</code></strong></p> <pre><code>public class ConfigSourcesConfigurer extends PropertySourcesPlaceholderConfigurer implements EnvironmentAware, InitializingBean { private Environment environment; private List&lt;PropertySource&gt; sourceList; // Allow setting property sources as a List for easier XML configuration public void setPropertySources(List&lt;PropertySource&gt; propertySources) { this.sourceList = propertySources; MutablePropertySources sources = new MutablePropertySources(); copyListToPropertySources(this.sourceList, sources); super.setPropertySources(sources); } @Override public void setEnvironment(Environment environment) { // save off Environment for later use this.environment = environment; super.setEnvironment(environment); } @Override public void afterPropertiesSet() throws Exception { // Copy property sources to Environment MutablePropertySources envPropSources = ((ConfigurableEnvironment)environment).getPropertySources(); copyListToPropertySources(this.sourceList, envPropSources); } private void copyListToPropertySources(List&lt;PropertySource&gt; list, MutablePropertySources sources) { // iterate in reverse order to insure ordering in property sources object for(int i = list.size() - 1; i &gt;= 0; i--) { sources.addFirst(list.get(i)); } } } </code></pre> <p><strong>beans.xml file showing basic configuration</strong></p> <pre><code>&lt;beans&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.mycompany" /&gt; &lt;bean class="com.mycompany.ConfigSourcesConfigurer"&gt; &lt;property name="propertySources"&gt; &lt;list&gt; &lt;bean class="org.mycompany.CustomPropertySource" /&gt; &lt;bean class="org.springframework.core.io.support.ResourcePropertySource"&gt; &lt;constructor-arg value="classpath:default-config.properties" /&gt; &lt;/bean&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="com.mycompany.TestBean"&gt; &lt;property name="stringValue" value="${placeholder}" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload