我们有一个@Configuration
类OurConfig文件,其bean被定义为@DependsOn("beanInsideAJar")@Bean
类OurBean
beanInsideAJar的定义在@Configuration
ConfigInAJar类中
并定义为@Bean
beanInsideAjar(SomeProperties someProperties)。 SomeProperties自动连接到beanInsideAjar。该ConfigInAJar类位于我们无法触摸/修改的库中。
在beanInsideAjar自动装配/使用它之前,如何覆盖SomeProperties中的属性值,而无需修改OurConfig文件?
我们尝试创建一个新的@Configuration
OverridingBean类并修改其中的SomeProperty值,然后将@DependsOn("OverridingBean")
添加到OurConfig类中。之所以有效,是因为OverridingBean首先在beanInsideAJar被实例化之前。
但是,我们有许多类似于OurConfig的@Configuration
类,我们避免将@DependsOn("OverridingBean")
添加到它们的每一个中。另外,我们不能直接修改SomeProperties类。
@Configuration
public class OurConfig {
@DependsOn("beanInsideAJar")
@Bean
public OurBean ourBean() {
@Configuration
public class ConfigInAJar {
@DependsOn("beanInsideAJar")
@Bean
public BeanInsideAJar beanInsideAJar(SomeProperties someProperties){
@Configuration
public class OverridingBean {
public OverridingBean(SomeProperties someProperties) {
someProperties.setPropertyA("hello");
}
}
@Configuration
@DependsOn("overridingBean") //we don't want to do this to all OurConfig files
public class OurConfig {
@DependsOn("beanInsideAJar")
@Bean
public OurBean ourBean() {