如何在运行时实例化 Spring 托管 Bean?

我坚持从普通的Java到Spring的简单重构。应用程序有一个“容器”对象,该对象在运行时实例化其部分。让我用代码解释一下:

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
    }
}

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

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

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

我们的对象是完全配置的,并注入了所有依赖项。但在我的情况下,我必须实例化一些对象,这些对象在执行load()方法后也需要依赖注入。
我怎样才能做到这一点?

我正在使用基于Java的配置。我已经尝试过为以下方面做一个工厂:RuntimeBeans

public class BeanRuntimeFactory {

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

期望在所谓的“精简”模式下工作。http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html不幸的是,我发现简单地做新的运行时Bean()没有区别;这里有一个类似问题的帖子:如何管理FactoryBean Spring创建的豆子?@Bean

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

我还尝试了AppplicationContext.getBean(“runtimeBean”,args),其中运行时Bean有一个“原型”范围,但getBean是一个糟糕的解决方案。


更新 1

更具体地说,我试图重构这个类:https://github.com/apache/lucene-solr/blob/trunk/solr/core/src/java/org/apache/solr/core/CoreContainer.java@see #load()方法并找到“return create(cd,false);”

更新 2

我在春季文档中发现了非常有趣的东西,称为“查找方法注入”:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection

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

更新 3

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

更新 4

所有这些“查找”方法的问题在于它们不支持传递任何参数。我还需要传递参数,就像我对app applicationContext.getBean(“runtimeBean”, arg1, arg2)所做的那样。看起来它在某些时候被修复了 https://jira.spring.io/browse/SPR-7431

更新 5

Google Guice有一个简洁的功能,称为AssistedInject。https://github.com/google/guice/wiki/AssistedInject


答案 1

看起来我找到了一个解决方案。由于我使用的是基于java的配置,因此它比您想象的还要简单。xml中的另一种方法是查找方法,但是只能从春季版本4.1.X开始,因为它支持将参数传递给方法。

下面是一个完整的工作示例:

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;
}

就是这样。

谢谢大家。


答案 2

我认为你的概念是错误的,因为你

绕过了Spring容器,并诉诸于使用常规的java构造函数,因此工厂方法的任何注释都被忽略了,这个bean永远不会被Spring管理。RuntimeBean beanRuntime = createRuntimeBean();

这是在一种方法中创建多个原型bean的解决方案,看起来不太好,但应该可以工作,我在运行时Bean中自动连接容器作为日志中显示的自动布线证明,您也可以在日志中看到每个Bean都是原型的新实例,当您运行此程序时。

'

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        Container container = (Container) context.getBean("container");
        container.load();
    }
}

@Component
class Container {
    private List<RuntimeBean> runtimeBeans = new ArrayList<RuntimeBean>();
    @Autowired
    ApplicationContext context;

    @Autowired
    private ObjectFactory<RuntimeBean> myBeanFactory;

    public void load() {

        // repeated several times depending on external data/environment
        for (int i = 0; i < 10; i++) {
            // **************************************
            // COMENTED OUT THE WRONG STUFFF 
            // RuntimeBean beanRuntime = context.getBean(RuntimeBean.class);
            // createRuntimeBean();
            // 
            // **************************************

            RuntimeBean beanRuntime = myBeanFactory.getObject();
            runtimeBeans.add(beanRuntime);
            System.out.println(beanRuntime + "  " + beanRuntime.container);
        }
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public RuntimeBean createRuntimeBean() {
        return new RuntimeBean();
    }
}

// @Component

class RuntimeBean {
    @Autowired
    Container container;

} '

推荐