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

我坚持从简单的java到spring的简单重构。 Application有一个“Container”对象,它在运行时实例化它的部分。 让我解释一下代码:

public class Container { private List runtimeBeans = new ArrayList(); 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的数量和配置的信息,然后根据给定的规范创建bean。

问题是:通常在spring做的时候

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

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

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

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

期待@Bean在所谓的’lite’模式下工作。 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html不幸的是,我发现简单地做新的RuntimeBean()没有区别; 这是一篇有类似问题的post: 如何通过FactoryBean spring管理创建的bean?

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

我还尝试了ApplicationContext.getBean(“runtimeBean”,args),其中runtimeBean具有“Prototype”范围,但getBean是一个糟糕的解决方案。

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

UPD2。 我在spring文档中发现了一个名为“查找方法注入”的非常有趣的东西: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method -注射

还有一个有趣的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)。

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

Upd4。 所有这些’lookup’方法的问题是它们不支持传递任何参数。我还需要像applicationContext.getBean(“runtimeBean”,arg1,arg2)那样传递参数。 看起来它已经通过https://jira.spring.io/browse/SPR-7431修复了

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

看起来我找到了解决方案。 因为我使用基于java的配置,它甚至比你想象的更简单。 xml中的替代方法是lookup-method,但仅限于spring version 4.1.X,因为它支持将参数传递给方法。

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

 public class Container { private List runtimeBeans = new ArrayList(); 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; } 

而已。

感谢大家。

我认为使用你的概念是错误的
RuntimeBean beanRuntime = createRuntimeBean();
你绕过Spring容器并使用常规的java构造函数,因此忽略了工厂方法的任何注释,这个bean永远不会被Spring管理

这里是在一个方法中创建多个原型bean的解决方案,不是很漂亮,但应该工作,我在RuntimeBean中自动assembly容器作为日志中显示的自动assembly的certificate,你也可以在日志中看到每个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 runtimeBeans = new ArrayList(); @Autowired ApplicationContext context; @Autowired private ObjectFactory 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; } ' 

您不需要Container因为ApplicationContext应创建,保存和管理所有运行时对象。 想想一个Web应用程序,它们大致相同。 如上所述,每个请求都包含外部数据/环境信息 。 你需要的是一个原型/请求范围的bean,如ExternalDataEnvironmentInfo ,它可以通过静态方式读取和保存运行时数据,比方说一个静态工厂方法。

       

如果确实需要一个容器来保存运行时对象,那么代码应该是

 class Container { List list; ApplicationContext context;//injected by spring if Container is not a prototype bean public void load() {// no loop inside, each time call load() will load a runtime object RuntimeBean bean = context.getBean(RuntimeBean.class); // see official doc list.add(bean);// do whatever } } 

具有原型bean依赖关系的官方doc Singleton bean 。