我正在编写一个Spring Boot Starter,我想在Spring IOC中实现Bean注入的动态创建,并且可以使@ConditionalOnMissingBean生效而无需创建默认Bean。
我试图直接在配置类中编织beanfactory,并通过ConfigurableBeanFactory调用registerSingleton方法来动态创建bean,但是结果是错误的,@ ConditionalOnMissingBean无效。@Bean可以,这应该与spring boot scan @Configuration类和相应的@Bean方法有关,但是@Bean无法实现Bean的动态创建,因为我需要的Bean是根据配置的内容生成的文件,数量不确定,内容不确定。
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
while (iterator.hasNext()) {
// Generate a bean based on the configuration
...
SpringBean bean = ...
configurableBeanFactory.registerSingleton(SpringBeanName, bean);
}
我希望如果动态创建bean,@ ConditionalOnMissingBean仍然会生效。
如API文档中所述:
@Conditional批注可以通过以下任何一种方式使用:
- 作为任何直接或间接用@Component注释的类的类型级别注释,包括@Configuration类
- 作为元注释,目的是组成自定义构造型注释
- 作为任何@Bean方法上的方法级注释
这意味着它对ApplicationContext
启动后在Bean中注册的动态创建的bean无效。因此,当您想要实现类似的行为时,请尝试检查bean是否存在于ApplicationContext中,如下所示:
@Autowired
private ApplicationContext context;
...
context.getBeansOfType(YourType.class).isEmpty()
如果要摆脱该bean的默认配置,则需要排除它。有两种方法可以实现这一目标。通过像指定一样@SpringBootApplication(exclude = SomeConfig.class)
排除定义bean的整个配置文件,或者在@ComponentScan上实现自己的排除过滤器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句