如何以编程方式将bean添加到Spring上下文?

时间:2017-03-25 20:18:40

标签: java spring

我想从某些配置创建自定义子上下文,并另外以编程方式添加一些bean。

我阅读了关于BeanDefinitionRegistryPostProcessor的回复https://stackoverflow.com/a/4540762/258483,但不明白如何使用它。如果我编写BeanDefinitionRegistryPostProcessor的实现,那么接下来该怎么做?加入上下文?但这是一个问题:如何将bean添加到上下文中!如果我能够将BeanDefinitionRegistryPostProcessor添加到上下文中,那么为什么我会问如何添加bean?

问题是我有上下文并想要添加bean。

我知道我可以实例化bean并使用

自动装配它们
Context#getAutowireCapableBeanFactory().createBean(klass);

但这显然只是连线类,但没有将它添加到上下文中?

2 个答案:

答案 0 :(得分:2)

Spring 5.0开始,您可以使用ApplicationContext直接动态注册bean。

GenericApplicationContext ac = ....;
// example
ac.registerBean("myspecialBean", Integer.class, () -> new Integer(100));
// using BeanDefinitionCustomizer
ac.registerBean("myLazySpecialBean", Integer.class, () -> new Integer(100), (bd) -> bd.setLazyInit(true));

有关registerBean

的不同API,请参阅此处的javadoc

答案 1 :(得分:0)

最初由我自己回答here,并在此重复。

  

实际上grep: /var/log/nginx/access.log*: No scuh file or directory 来自AnnotationConfigApplicationContext,其中有AbstractApplicationContext方法留空以进行覆盖

postProcessBeanFactory

要利用此功能,请创建/** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for registering special * BeanPostProcessors etc in certain ApplicationContext implementations. * @param beanFactory the bean factory used by the application context */ protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { } 类,其类似于以下内容(针对AnnotationConfigApplicationContextProvider实例示例,您可以使用Vertx代替...)

MyClass

创建public class CustomAnnotationApplicationContextProvider { private final Vertx vertx; public CustomAnnotationApplicationContextProvider(Vertx vertx) { this.vertx = vertx; } /** * Register all beans to spring bean factory * * @param beanFactory, spring bean factory to register your instances */ private void configureBeans(ConfigurableListableBeanFactory beanFactory) { beanFactory.registerSingleton("vertx", vertx); } /** * Proxy method to create {@link AnnotationConfigApplicationContext} instance with no params * * @return {@link AnnotationConfigApplicationContext} instance */ public AnnotationConfigApplicationContext get() { return new AnnotationConfigApplicationContext() { @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); configureBeans(beanFactory); } }; } /** * Proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(DefaultListableBeanFactory)} with our logic * * @param beanFactory bean factory for spring * @return * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(DefaultListableBeanFactory) */ public AnnotationConfigApplicationContext get(DefaultListableBeanFactory beanFactory) { return new AnnotationConfigApplicationContext(beanFactory) { @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); configureBeans(beanFactory); } }; } /** * Proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(Class[])} with our logic * * @param annotatedClasses, set of annotated classes for spring * @return * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(Class[]) */ public AnnotationConfigApplicationContext get(Class<?>... annotatedClasses) { return new AnnotationConfigApplicationContext(annotatedClasses) { @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); configureBeans(beanFactory); } }; } /** * proxy method to call {@link AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(String...)} with our logic * * @param basePackages set of base packages for spring * @return * @see AnnotationConfigApplicationContext#AnnotationConfigApplicationContext(String...) */ public AnnotationConfigApplicationContext get(String... basePackages) { return new AnnotationConfigApplicationContext(basePackages) { @Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.postProcessBeanFactory(beanFactory); configureBeans(beanFactory); } }; } } 时,您可以使用

创建它
ApplicationContext