通过@Import覆盖Spring @PropertySource

时间:2013-03-22 18:09:07

标签: spring properties

我在DefaultConfig类中有一个属性test=default,我正在使用@PropertySource注释使它们可用。

@Configuration
@PropertySource("classpath:default.properties")
public class DefaultConfig {}

然后我希望能够覆盖到test=override,它位于OverrideConfig类的不同属性文件中,所以我再次使用@PropertySource。

@Configuration
@Import(DefaultConfig.class)
@PropertySource("classpath:override.properties")
public class OverrideConfig {}

我配置测试以证明它有效。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={OverrideConfig.class})
public class TestPropertyOverride {

    @Autowired
    private Environment env;

    @Test
    public void propertyIsOverridden() {
        assertEquals("override", env.getProperty("test"));
    }

}

除了当然没有。

org.junit.ComparisonFailure: expected:<[override]> but was:<[default]>

最大化调试,我可以看到发生了什么:

StandardEnvironment:107 - Adding [class path resource [default.properties]] PropertySource with lowest search precedence
StandardEnvironment:107 - Adding [class path resource [override.properties]] PropertySource with lowest search precedence

似乎倒退了。我是否犯了一个简单的错误或者误解了这个问题,或者您是否希望@ Import-ed类中的@PropertySource定义的属性被@ Import-ing类中的@@ @ @ @ @ @ @ @ @ppertySource中定义的属性覆盖?

5 个答案:

答案 0 :(得分:6)

以下是Helder Sousa的解决方案,由OP创建的a comment of the JIRA issue编写:

  

[T]使用嵌套配置可以实现spring xml中的行为(一个xml导入另一个xml):

@Configuration
@PropertySource("classpath:default.properties")
public class DefaultConfig {}
     
@Configuration
@PropertySource("classpath:override.properties")
public class OverrideConfig {

    @Configuration
    @Import(DefaultConfig.class)
    static class InnerConfiguration {}

}
     

使用此设置,属性将按正确顺序收集。

答案 1 :(得分:2)

今天使用Spring 4你可以使用它:

@TestPropertySource(value="classpath:/config/test.properties")

这可用于使用并最终覆盖junit测试的属性:

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(value="classpath:/config/test.properties")

答案 2 :(得分:1)

我目前在Spring 3.1中遇到类似案例,但我使用不同的方法覆盖属性,因为@PropertySource不支持可选属性文件:

@Configuration
@PropertySource("classpath:default.properties")
public class BaseConfig {

  @Inject
  private ApplicationContext context;

  @PostConstruct
  public void init() throws IOException {
    Resource runtimeProps = context.getResource("classpath:override.properties");
    if (runtimeProps.exists()) {
      MutablePropertySources sources = ((ConfigurableApplicationContext) context).getEnvironment().getPropertySources();
      sources.addFirst(new ResourcePropertySource(runtimeProps));
    }
  }
...

除了普通bean依赖项所规定的顺序之外,@Import似乎不会导致@Configuration实例化的任何特定顺序。强制执行此类顺序的一种方法是将基本@Configuration实例本身作为依赖项注入。你能尝试一下:

@Configuration
@Import(DefaultConfig.class)
@PropertySource("classpath:override.properties")
public class OverrideConfig {

  @Inject
  private DefaultConfig defaultConfig;

  ...
}

这有帮助吗? 也许新的ContextHierarchy注释也可以在这里提供帮助,但到目前为止我还没有尝试过这个。

答案 3 :(得分:0)

您可以执行以下属性的加载顺序:

@Configuration
@PropertySource(value={"classpath:default.properties","classpath:override.properties"})
public class OverrideConfig {
...
}

答案 4 :(得分:0)

我遇到了类似的问题并且成功地在我的自定义配置中声明了默认属性:

@Configuration
@Import(DefaultConfig.class)
@PropertySource({"classpath:default.properties", "classpath:override.properties"})
public class OverrideConfig {}
相关问题