gpt4 book ai didi

java - 什么是 Spring 的注解对应物 ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:19 27 4
gpt4 key购买 nike

我们可以使用 <context:property-placeholder> 外部化属性我们可以通过配置 <context:property-override> 来覆盖 Spring bean 属性如下:

<context:property-placeholder location="classpath:application.properties"/>
<context:property-override location="classpath:override.properties"/>

我想将我的 XML 配置移动到 JavaConfig。

@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
public class AppConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

}

但是如何使用注释配置我的覆盖属性?

附言:我有一个 bean 说 MyBean 如下:

@Component
public class MyBean {

@Value("${someProp}")
private String someProp;
}

在我的 application.properties我有

someProp=TestValue

在我的 override.properties我正在重写 someProp 值作为

myBean.someProp=RealValue

最佳答案

不,不是。

但是您可以在配置类中创建类型为 PropertyOverrideConfigurer 的 bean,结果相同。

更新

例如:

@Bean public static PropertyOverrideConfigurer  propertyOverrideConfigurer() { 
PropertyOverrideConfigurer overrideConfigurer = new PropertyOverrideConfigurer();
overrideConfigurer.setLocation(new ClassPathResource("override.properties"));
return overrideConfigurer;
}

注意 static 修饰符,这是因为 BFPP 应该在容器生命周期的早期实例化。

参见 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html了解更多信息。

关于java - 什么是 Spring 的注解对应物 <context :property-override>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com