3

I have the following Spring XML-based config:

<!--
  These properties define the db.type property.
-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  <property name="order" value="1"/>
  <property name="ignoreUnresolvablePlaceholders" value="true" />
  <property name="ignoreResourceNotFound" value="true" />
  <property name="locations">
    <list>
      <!-- contains default db.type -->
      <value>classpath:/default-cfg.properties</value>
      <!-- db.type can be overridden by optional properties in the app work dir -->
      <value>file:${app.work.dir}/cfg.properties</value>
    </list>
  </property>
</bean>

<!-- db.type should be used to get DB specific config properties -->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  <property name="order" value="2"/>
  <property name="ignoreUnresolvablePlaceholders" value="false" />
  <property name="ignoreResourceNotFound" value="false" />
  <property name="locations">
    <list>
      <!-- does not work, ${db.type} does not get resolved -->
      <value>classpath:/default-cfg-${db.type}.properties</value>
    </list>
  </property>
</bean>

The problem is that the property in the classpath:/default-cfg-${db.type}.properties location does not get resolved even though db.type is defined by the first property placeholder configurer.

I keep getting the following error in the log:

 class path resource [default-cfg-${db.type}.properties] cannot be opened because it does not exist

Is there any way to use properties in the PropertyPlaceholderConfigurer's locations attribute? I want to avoid using JVM system property to set the db.type value.

Thank you, Jan

4

2 回答 2

2

由于 Alireza Fattahi 要求提供我的代码并且 WornOutSoles 建议接受的解决方案不起作用,因此我发布了基于 Spring ApplicationContextInitializer 的最终解决方案:

public class SpringProfilesActivator
    implements ApplicationContextInitializer<XmlWebApplicationContext>
{
  private static final Logger log = LoggerFactory.getLogger( SpringProfilesActivator.class );

  ...

  @Override
  public void initialize( XmlWebApplicationContext applicationContext )
  {
    // TODO: get your profiles from somewhere (config files, JVM system properties, database etc.)
    String[] activeProfiles = new String[] { ... };

    log.info( "Activating Spring profiles: {}", Arrays.toString( activeProfiles ) );

    ConfigurableEnvironment env = applicationContext.getEnvironment();
    env.setActiveProfiles( activeProfiles );
  }

  ...
}
于 2014-12-30T14:22:21.603 回答
0

两个后处理器都被实例化,排序然后执行,所以你的第二个PSPC在第一个执行之前被实例化。

如果您的第一个PSPC工具BeanDefinitionRegistryPostProcessor将在第二个工具实例化之前运行,并且将按您的预期工作。

例如:

public class BeanDefinitionRegistryPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer 
    implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // nothing todo, just to run before other PPHCs are instantiated
    }
}
于 2013-05-14T15:29:52.900 回答