使用 @Configuration 在 Spring 中创建 bean 集合
如何使用带有 @Configuration 注释的类创建由 Spring 正确管理的 bean 集合。
我想做这样的事情:
@Configuration
public Config {
@Autowired
private SomeConfiguration config;
@Bean
public List<MyBean> myBeans() {
List<MyBean> beans = new ArrayList<MyBean>();
for (Device device : config.getDevices()) {
beans.add(new MyBean(device));
}
return beans;
}
}
但是 MyBean 实例不会进行后期处理。因此,它们的 @Autowired 方法不会被调用,bean 不会注册为 mbean 等。但是该列表是可访问的,以便我可以自动装配 MyBean 对象的列表。
我不能使用类似的东西:
@Configuration
public Config {
@Autowired
private SomeConfiguration config;
@Bean
public MyBean myBean1() { ... }
@Bean
public MyBean myBean2() { ... }
}
因为 MyBean 实例的数量在运行时之前是未知的。我想这样做的原因是因为我们正在控制具有可变数量组件的物理机器。我希望每个组件有一个 bean。
我目前正在通过使用这样的 BeanFactoryPostProcessor 来实现我们的目标:
@Component
public class MyBeansFactoryPostProcessor implements BeanFactoryPostProcessor {
@Autowired
private SomeConfiguration config;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeanException {
for (Device device : config.getDevices()) {
createAndRegister(BeanDefinitionRegistry) beanFactory, device);
}
}
private void createAndRegister(BeanDefinitionRegistry registry, Device device) {
register.registerBeanDefinition("device" + device.getId(), BeanDefinitionBuilder.genericBeanDefinition(MyBean.class).addConstructorArgValue(device).getBeanDefinition());
}
}
但这感觉就像一个非常丑陋的黑客。
How can I create a collection of beans that will be properly managed by Spring using a class with a @Configuration annotation.
I would like to do something like this:
@Configuration
public Config {
@Autowired
private SomeConfiguration config;
@Bean
public List<MyBean> myBeans() {
List<MyBean> beans = new ArrayList<MyBean>();
for (Device device : config.getDevices()) {
beans.add(new MyBean(device));
}
return beans;
}
}
But the MyBean instances aren't post processed. So their @Autowired methods are not called, the beans are not registered as mbeans and etc. The list is however accessible so that I can autowire a List of MyBean objects.
I cannot use something like:
@Configuration
public Config {
@Autowired
private SomeConfiguration config;
@Bean
public MyBean myBean1() { ... }
@Bean
public MyBean myBean2() { ... }
}
Since the number of MyBean instances are not known before runtime. The reason I want to do this is because we are controlling a physical machine that have a variable amount of components. And I want to have one bean per component.
I'm currently achieving our goal by using a BeanFactoryPostProcessor like this:
@Component
public class MyBeansFactoryPostProcessor implements BeanFactoryPostProcessor {
@Autowired
private SomeConfiguration config;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeanException {
for (Device device : config.getDevices()) {
createAndRegister(BeanDefinitionRegistry) beanFactory, device);
}
}
private void createAndRegister(BeanDefinitionRegistry registry, Device device) {
register.registerBeanDefinition("device" + device.getId(), BeanDefinitionBuilder.genericBeanDefinition(MyBean.class).addConstructorArgValue(device).getBeanDefinition());
}
}
But this just feels like a really ugly hack.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用支持
SmartLifecycle
的ConfigurableListableBeanFactory
,因此,如果您在应用完全初始化之前注册该 bean,它将为您调用start()
并其他后处理步骤。然而 - 如果你在 spring 初始化后调用
beanFactory.registerSingleton
,你将需要手动调用start()
- 好的一面是,尽管你的 bean 仍然完全连接到生命周期中当应用程序上下文关闭时,管理和 spring 将为您调用stop()
。You can use the
ConfigurableListableBeanFactory
which supportsSmartLifecycle
so if you register the bean before your app is fully initialized it will callstart()
for you and other post processing steps.However - if you call
beanFactory.registerSingleton
after spring has initialized you will manually need to callstart()
- on the bright side though you bean is still fully wired into the lifecycle management and spring will callstop()
for you when the application context is shutdown.我相信在这种情况下,另一个选择是按以下方式使用
@PostConstruct
:@PostConstruct
注释对于初始化属性很有用。它保证带注释的方法只会在创建 bean 时被调用一次。如果我错了请纠正我
I believe that another option in this case is to use
@PostConstruct
in the following manner:The
@PostConstruct
annotation is useful for initializing properties. It guarantees that the annotated method will only be called once when the bean is created.Correct me if I'm wrong
为了注入您的
MyBean
列表,请尝试使用@Resource
而不是@Autowired
。例如In order to inject your
MyBean
list try@Resource
instead of@Autowired
. for e.g.不可能使用
@Configuration
为每个方法定义多个 bean (AFAIK)。因此,您必须继续使用 BFPP 或使用ApplicationContect.getAutowireCapableBeanFactory().autowire(object);
It's not possible using
@Configuration
to define more than one bean per method (AFAIK). So you will have to contnue using a BFPP or useApplicationContect.getAutowireCapableBeanFactory().autowire(object);
MyBeans
不会进行后处理,因为它们是使用new
创建的,并且不是由 Spring 容器初始化的。您需要使用原型 bean,每个组件发出的请求都有一个新 bean 的实例。
您需要标记您的
MyBean(Device device)
bean 声明,例如@Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
然后调用它而不是使用
new
填充bean
的位置。MyBeans
are not post processed as they are created withnew
and not initialised by the Spring container.You need to use prototype beans, have an instance of a new bean per request made by a component.
You will need to tag your
MyBean(Device device)
bean declaration like@Bean @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
Then call that instead of using
new
where you populatebeans
.我最终扩展了 ArrayList。
当然,这有点老套,但感觉比提问者的方法要好一些。
I've ended up extending the ArrayList.
This is, of course, hacky but feels a less uglier than the questioners approach.