我坚持从纯 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
的数量和配置的信息,然后根据给定的规范创建 bean。
问题是:通常我们在Spring做的时候
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
Container container = (Container) context.getBean("container");
我们的对象已完全配置并注入(inject)了所有依赖项。但在我的情况下,我必须在执行 load() 方法后实例化一些也需要依赖注入(inject)的对象。
我怎样才能做到这一点?
我正在使用基于 Java 的配置。我已经尝试为 RuntimeBeans
制作工厂:
public class BeanRuntimeFactory {
@Bean
public RuntimeBean createRuntimeBean() {
return new RuntimeBean();
}
}
期望 @Bean
在所谓的“精简”模式下工作。 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html不幸的是,我发现简单地做 new RuntimeBean(); 没有区别。
这是一个类似问题的帖子:How to get beans created by FactoryBean spring managed?
还有http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Configurable.html但在我看来,它就像一把锤子。
我还尝试了 ApplicationContext.getBean("runtimeBean", args) 其中 runtimeBean 具有“原型(prototype)”范围,但 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
我在 Spring 文档中发现了一个非常有趣的东西,叫做“查找方法注入(inject)”: 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菲尔韦伯说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
更新 4
所有这些“查找”方法的问题是它们不支持传递任何参数。我还需要传递参数,就像我对 applicationContext.getBean("runtimeBean", arg1, arg2) 所做的那样。看起来它在某个时候用 https://jira.spring.io/browse/SPR-7431 修复了
更新 5
Google Guice 有一个名为 AssistedInject 的简洁功能。 https://github.com/google/guice/wiki/AssistedInject
最佳答案
看来我找到了解决方案。由于我使用的是基于 java 的配置,它比您想象的还要简单。 xml 中的替代方法是查找方法,但仅从 spring 版本 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;
}
就是这样。
谢谢大家。
关于java - 如何在运行时实例化 Spring 托管 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809838/