如何在运行时实例化Spring受管Bean? [英] How to instantiate spring managed beans at runtime?

查看:222
本文介绍了如何在运行时实例化Spring受管Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持一个简单的重构从简单的java到春天。应用程序有一个容器对象,它在运行时实例化其部件。让我解释一下代码:

  public class Container {
private List< RuntimeBean> runtimeBeans = new ArrayList< RuntimeBean>();

public void load(){
//根据外部数据/环境重复几次
RuntimeBean beanRuntime = createRuntimeBean();
runtimeBeans.add(beanRuntime);
}

public RuntimeBean createRuntimeBean(){
//应该创建bean,内部可以有一些
//弹簧注释,换句话说,
/ /应该由spring
}
}

基本上,在加载容器请求一些外部系统向他提供有关每个RuntimeBean的数量和配置的信息,然后根据给定的规格创建bean。



问题是:通常当我们在春天

  ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); 
容器容器=(容器)context.getBean(container);

我们的对象已完全配置,并注入了所有依赖项。但是在我的情况下,我必须实例化一些在执行load()方法后也需要依赖注入的对象。
我该如何实现?



我正在使用基于java的配置。我已经尝试为RuntimeBeans制作一个工厂:

  public class BeanRuntimeFactory {

@Bean
public RuntimeBean createRuntimeBean(){
return new RuntimeBean();
}
}

expecting @Bean在所谓的lite模式。 http://docs.spring 。/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
这是一个类似问题的帖子:
如何获得由FactoryBean spring创建的bean管理?



还有一个 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ bean / factory / annotation / Configurable.html ,但在我的情况下看起来像是一把锤子。



我也尝试过ApplicationContext.getBean(runtimeBean,args )其中runtimeBean有一个Prototype范围,但getBean是一个很糟糕的解决方案。



Upd1。
为了更具体一些我试图重构这个类:
https://github.com/apache/lucene-solr/blob/trunk/solr/core/s rc / java / org / apache / solr / core / CoreContainer.java
@see #load()方法并找到return create(cd,false);



Upd2。
我在spring文档中发现了非常有趣的东西叫做lookup method injection:
http://docs.spring.io/spring/ docs / current / spring-framework-reference / html / beans.html#beans-factory-lookup-method-injection



还有一个有趣的jira票Phil Webb说的 https://jira.spring.io/browse/SPR-5192 https://jira.spring.io/browse/SPR-5192?focusedCommentId=86051&page=com.atlassian.jira.plugin.system.issuetabp anels:comment-tabpanel#comment-86051 应该在这里使用javax.inject.Provider(它提醒我Guice)。



Upd3。
还有一个 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.html



Upd4。
所有这些查找方法的问题是它们不支持传递任何参数。我还需要传递参数使用applicationContext.getBean(runtimeBean,arg1,arg2)。在 https://jira.spring.io/browse/SPR- 7431



Upd5。 Google Guice具有称为AssistedInject的整洁功能。 https://github.com/google/guice/wiki/AssistedInject

解决方案

看起来我发现了一个解决方案。因为我使用基于java的配置,它比你想像的要简单得多。 xml中的替代方法是lookup-method,但是只能从spring version 4.1.X,因为它支持传递方法的参数。



这是一个完整的工作示例: / p>

  public class Container {
private List&RuntimeBean> runtimeBeans = new ArrayList< RuntimeBean>();
private RuntimeBeanFactory runtimeBeanFactory;

public void load(){
//根据外部数据/环境重复几次
runtimeBeans.add(createRuntimeBean(Some external info1));
runtimeBeans.add(createRuntimeBean(Some external info2));
}

public RuntimeBean createRuntimeBean(String info){
//应该创建内部可以有一些
//弹簧注释的bean,换句话说就是
//应该由spring
返回runtimeBeanFactory.createRuntimeBean(info)
}

public void setRuntimeBeanFactory(RuntimeBeanFactory runtimeBeanFactory){
this.runtimeBeanFactory = runtimeBeanFactory
}
}

public interface RuntimeBeanFactory {
RuntimeBean createRuntimeBean(String info);
}

//最后
@Configuration
public class ApplicationConfiguration {

@Bean
容器容器(){
容器容器=新容器(beanToInject());
container.setBeanRuntimeFactory(runtimeBeanFactory());
返回容器;
}

//看看它在JAVA配置中如何简单
@Bean
public BeanRuntimeFactory runtimeBeanFactory(){
return new BeanRuntimeFactory(){
public RuntimeBean createRuntimeBean(String beanName){
return runtimeBean(beanName);
}
};
}

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
RuntimeBean runtimeBean(String beanName){
返回新的RuntimeBean(beanName);
}
}

class RuntimeBean {
@Autowired
容器容器;
}

就是这样。



谢谢大家。


I stuck with a simple refactoring from plain java to spring. Application has a "Container" object which instantiates its parts at runtime. Let me explain with the code:

public class Container {
    private List<RuntimeBean> runtimeBeans = new ArrayList<RuntimeBean>();

    public void load() {
        // repeated several times depending on external data/environment
        RuntimeBean beanRuntime = createRuntimeBean();
        runtimeBeans.add(beanRuntime);
    }

    public RuntimeBean createRuntimeBean() {
         // should create bean which internally can have some 
         // spring annotations or in other words
         // should be managed by spring
    }
}

Basically, during load container asks some external system to provide him information about number and configuration of each RuntimeBean and then it create beans according to given spec.

The problem is: usually when we do in spring

ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
Container container = (Container) context.getBean("container");

our object is fully configured and have all dependencies injected. But in my case I have to instantiate some objects which also needs dependency injection after I execute load() method. How can I achieve that?

I am using java-based config. I already tried making a factory for RuntimeBeans:

public class BeanRuntimeFactory {

    @Bean
    public RuntimeBean createRuntimeBean() {
        return new RuntimeBean();
    }
}

expecting @Bean to work in so called 'lite' mode. http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html Unfortunately, I found no difference with simply doing new RuntimeBean(); Here is a post with a similar issue: How to get beans created by FactoryBean spring managed?

There is also http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Configurable.html but it looks like a hammer in my case.

I also tried ApplicationContext.getBean("runtimeBean", args) where runtimeBean has a "Prototype" scope, but getBean is an awful solution.

Upd1. To be more concrete I am trying to refactor this class: https://github.com/apache/lucene-solr/blob/trunk/solr/core/src/java/org/apache/solr/core/CoreContainer.java @see #load() method and find "return create(cd, false);"

Upd2. I found quite interesting thing called "lookup method injection" in spring documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection

And also an interesting jira ticket https://jira.spring.io/browse/SPR-5192 where Phil Webb says https://jira.spring.io/browse/SPR-5192?focusedCommentId=86051&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-86051 that javax.inject.Provider should be used here (it reminds me Guice).

Upd3. There is also http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/ServiceLocatorFactoryBean.html

Upd4. The issue with all these 'lookup' methods is they don't support passing any arguments.. I also need to pass arguments as I would do with applicationContext.getBean("runtimeBean", arg1, arg2). Looks like it was fixed at some point with https://jira.spring.io/browse/SPR-7431

Upd5. Google Guice have a neat feature for it called AssistedInject. https://github.com/google/guice/wiki/AssistedInject

解决方案

Looks like I found a solution. As I am using java based configuration it is even simpler than you can imagine. Alternative way in xml would be lookup-method, however only from spring version 4.1.X as it supports passing arguments to the method.

Here is a complete working example:

public class Container {
    private List<RuntimeBean> runtimeBeans = new ArrayList<RuntimeBean>();
    private RuntimeBeanFactory runtimeBeanFactory;

    public void load() {
        // repeated several times depending on external data/environment
        runtimeBeans.add(createRuntimeBean("Some external info1"));
        runtimeBeans.add(createRuntimeBean("Some external info2"));
    }

    public RuntimeBean createRuntimeBean(String info) {
         // should create bean which internally can have some 
         // spring annotations or in other words
         // should be managed by spring
         return runtimeBeanFactory.createRuntimeBean(info)
    }

    public void setRuntimeBeanFactory(RuntimeBeanFactory runtimeBeanFactory) {
        this.runtimeBeanFactory = runtimeBeanFactory
    }
}

public interface RuntimeBeanFactory {
    RuntimeBean createRuntimeBean(String info);
}

//and finally
@Configuration
public class ApplicationConfiguration {

    @Bean
    Container container() {
        Container container = new Container(beanToInject());
        container.setBeanRuntimeFactory(runtimeBeanFactory());
        return container;
    }

    // LOOK HOW IT IS SIMPLE IN THE JAVA CONFIGURATION
    @Bean 
    public BeanRuntimeFactory runtimeBeanFactory() {
        return new BeanRuntimeFactory() {
            public RuntimeBean createRuntimeBean(String beanName) {
                return runtimeBean(beanName);
            }
        };
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    RuntimeBean runtimeBean(String beanName) {
        return new RuntimeBean(beanName);
    }
}

class RuntimeBean {
    @Autowired
    Container container;
}

That's it.

Thanks everyone.

这篇关于如何在运行时实例化Spring受管Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆