作者:mobiledu2502894873 | 来源:互联网 | 2023-05-28 09:05
我坚持从简单的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.
问题是:通常在春天做的时候
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();
}
}
期待@Bean在所谓的'lite'模式下工作.http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html不幸的是,我发现只是做新的RuntimeBean()没有区别; 这是一篇有类似问题的帖子:如何获得由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的简洁功能.https://github.com/google/guice/wiki/AssistedInject
1> Vadim Kirilc..:
看起来我找到了解决方案.因为我使用基于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;
}
而已.
感谢大家.
在不接受构造函数参数之前,自4.1.4起已对其进行了修复。您必须通过调用return runtimeBean(beanName);来记住这一点。您不是直接调用方法runtimeBean,而是在负责在Spring上下文中创建此bean的bean工厂上调用实例化方法,并且参数在传递给实际bean之前由工厂解析。和“ @Configuration”注释,整个过程被ConfigurationClassEnhancer.BeanFactoryAwareMethodInterceptor拦截,它决定如何实例化bean。