我有以下bean /构造函数定义:
@Configuration
class Configuration {
@Bean
public List<Something> getSomethings(MyFancyStuff stuff, @Autowired Bar bar) {
//...
}
}
@Component
class SomeOtherThing {
public SomeOtherThing(MyFancyStuff stuff, @Autowired Bar bar) {
//...
}
}
当为给定参数找到特定的类或注释时,是否可以扩展依赖关系解析以提供自定义解析器?我查看了PropertyPlaceholderConfigurer
和InstantiationAwareBeanPostProcessor
,但似乎没有任何帮助我编写自己的价值提供者。
作为上下文:我实现了一个自定义作用域,它为每个配置对象创建给定bean的许多实例。我想将此配置对象传递给所述范围的bean创建过程,而不将其添加到应用程序上下文中。我不想将它添加到应用程序上下文中,因为它是一个没有其他对象应该能够通过依赖注入获得的对象。我需要扩展spring的DI过程,因为我想支持字段注入,构造函数注入和bean工厂方法,如显示的getSomethings
注意:这与SpringMVC请求参数的自动值转换无关。
答案 0 :(得分:0)
您可以使用@Conditional Annotation
@Bean(name="dataSource")
@Conditional(value=DevCondition.class)
public Util getSource1() {
return new DevUtil();
}
@Bean(name="dataSource")
@Conditional(ProdCondition.class)
public Util getSource2() {
return new ProdUtil();
}
您还可以使用@Profile
基于属性值创建bean