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

How to instantiate Spring managed beans at runtime?

我坚持从简单的 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");

我们的对象已完全配置并注入了所有依赖项。但在我的例子中,我必须在执行 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 不幸的是,我发现简单地执行 new RuntimeBean(); 没有任何区别; 这是一个具有类似问题的 post: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 有一个“原型”范围,但是 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 文档中发现了一个叫做“查找方法注入”的非常有趣的东西: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection

还有一个有趣的 jira ticket https://jira.spring.io/browse/SPR-5192 where Phil Webb says 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

所有这些 'lookup' 方法的问题是它们不支持传递任何参数。我还需要像使用 applicationContext.getBean("runtimeBean", arg1,参数 2)。看起来它在某些时候已用 https://jira.spring.io/browse/SPR-7431

修复

更新 5

Google Guice 有一个名为 AssistedInject 的巧妙功能。 https://github.com/google/guice/wiki/AssistedInject

我认为你的概念是错误的
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;

} '

看来我找到了解决办法。由于我使用的是基于 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;
}

就是这样。

谢谢大家

您不需要 Container,因为所有运行时对象都应由 ApplicationContext 创建、保存和管理。想想 Web 应用程序,它们非常相似。正如您上面提到的,每个请求都包含 external data/environment info。您需要的是一个 prototype/request 作用域 bean,例如 ExternalDataEnvironmentInfo,它可以通过 static 方式读取和保存运行时数据,比方说静态工厂方法。

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

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

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 的依赖注入,因为 post 处理器在实际的应用程序上下文中工作。

一个简单的方法:

@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