[英]Inject list of initialized beans
I need to inject list of already initialized beans into some another one. 我需要将已经初始化的bean列表注入另一个。
I have class with definitions of some lazy beans which are used depending on environment - like on server 1 only impl1
and impl2
will be used and on server 2 impl3
and impl1
我有一些根据环境使用的懒豆的定义的类-例如在服务器1上仅使用
impl1
和impl2
,在服务器2上使用impl3
和impl1
@Component
class Definitions {
@Bean
@Lazy
public A impl1() { /* ... */ }
@Bean
@Lazy
public A impl2() { /* ... */ }
@Bean
@Lazy
public A impl3() { /* ... */ }
}
And I have some monitoring bean which don't know anything about environment and just collects all those A
beans exposing some health information for actuator: 我有一些监视bean,它对环境一无所知,只是收集了所有那些为执行器公开一些健康信息的
A
bean:
@Component
class Monitoring implements HealthIndicator {
@Autowired
private List<A> monitored;
}
Problem is that spring wires all beans into monitored
even if they were not initialized before (which crashes the whole thing, cause there is no suitable environment). 问题在于,即使没有对它们进行初始化,弹簧也会将所有bean连接到
monitored
(这会使整个事情崩溃,导致没有合适的环境)。 And I need to somehow explain to spring that I only need already initialized beans - something like @AutowireOnlyThoseLazyBeansWhichAlreadyBeenUsedSomewhereElse
而且我需要以某种方式向春天解释我只需要已经初始化的
@AutowireOnlyThoseLazyBeansWhichAlreadyBeenUsedSomewhereElse
就像@AutowireOnlyThoseLazyBeansWhichAlreadyBeenUsedSomewhereElse
PS I know that I can use dirty hack and declare a list property inside Definitions
, fill it in bean factory methods and register another one bean with reference to that list but it is too dirty. PS我知道我可以使用肮脏的hack并在
Definitions
声明一个list属性,将其填充到bean工厂方法中,并引用该列表注册另一个bean,但这太脏了。
解决方案是通过监视BeanPostProcessor某种特定类型的bean,并将动态bean与并发映射中的所有跟踪bean注册到上下文中,来编写类似于OSGi的自定义“ ServiceTracker”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.