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

时间:2015-01-07 00:55:21

标签: java spring dependency-injection refactoring guice

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

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。

问题是:通常在春天做的时候

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()没有区别; 这是一篇有类似问题的帖子: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是一个糟糕的解决方案。

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

UPD2。 我在spring文档中发现了一个非常有趣的“查找方法注入”: 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)。

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

5 个答案:

答案 0 :(得分:5)

看起来我找到了解决方案。因为我使用基于java的配置,它甚至比你想象的更简单。 xml中的替代方法是lookup-method,但仅限于spring version 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;
}

那就是它。

谢谢大家。

答案 1 :(得分:2)

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

这里是在一个方法中创建多个原型bean的解决方案,不是很漂亮,但应该工作,我在RuntimeBean中自动装配容器作为日志中显示的自动装配的证明,你也可以在日志中看到每个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;

} '

答案 2 :(得分:2)

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

<bean id="externalData" class="ExternalData"
    factory-method="read" scope="prototype"></bean>

<bean id="environmentInfo" class="EnvironmentInfo"
    factory-method="read" scope="prototype/singleton"></bean>

<bean class="RuntimeBean" scope="prototype">
    <property name="externalData" ref="externalData">
    <property name="environmentInfo" ref="environmentInfo">
</bean> 

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

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

官方文件Singleton beans with prototype-bean dependencies

答案 3 :(得分:1)

可以使用BeanFactoryPostProcesor动态注册bean。在这里,您可以在应用程序启动时执行此操作(spring的应用程序上下文正在初始化)。您不能注册bean乳胶,但是另一方面,您可以对bean使用依赖注入,因为它们成为“真正的” Spring bean。

public class DynamicBeansRegistar implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (! (beanFactory instanceof BeanDefinitionRegistry))  {
            throw new RuntimeException("BeanFactory is not instance of BeanDefinitionRegistry);
        }   
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

        // here you can fire your logic to get definition for your beans at runtime and 
        // then register all beans you need (possibly inside a loop)

        BeanDefinition dynamicBean = BeanDefinitionBuilder.    
             .rootBeanDefinition(TheClassOfYourDynamicBean.class) // here you define the class
             .setScope(BeanDefinition.SCOPE_SINGLETON)
             .addDependsOn("someOtherBean") // make sure all other needed beans are initialized

             // you can set factory method, constructor args using other methods of this builder

             .getBeanDefinition();

        registry.registerBeanDefinition("your.bean.name", dynamicBean);           

}

@Component
class SomeOtherClass {

    // NOTE: it is possible to autowire the bean
    @Autowired
    private TheClassOfYourDynamicBean myDynamicBean;

}

如上所述,您仍然可以使用Spring的依赖注入,因为后处理器可以在实际的应用程序上下文中工作。

答案 4 :(得分:1)

一种简单的方法:

@Component
public class RuntimeBeanBuilder {

    @Autowired
    private ApplicationContext applicationContext;

    public MyObject load(String beanName, MyObject myObject) {
        ConfigurableApplicationContext configContext = (ConfigurableApplicationContext) applicationContext;
        SingletonBeanRegistry beanRegistry = configContext.getBeanFactory();

        if (beanRegistry.containsSingleton(beanName)) {
            return beanRegistry.getSingleton(beanName);
        } else {
            beanRegistry.registerSingleton(beanName, myObject);

            return beanRegistry.getSingleton(beanName);
        }
    }
}


@Service
public MyService{

   //inject your builder and create or load beans
   @Autowired
   private RuntimeBeanBuilder builder;

   //do something
}

您可以使用以下方法来代替使用SingletonBeanRegistry:

BeanFactory beanFactory = configContext.getBeanFactory();

无论如何,SingletonBeanBuilder扩展了HierarchicalBeanFactory,而HierarchicalBeanFactory扩展了BeanFactory