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

Vad*_*huk 14 java refactoring spring dependency-injection 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
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,在加载容器期间要求一些外部系统向他提供有关每个RuntimeBean的数量和配置的信息,然后根据给定的规范创建bean.

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

ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
Container container = (Container) context.getBean("container");
Run Code Online (Sandbox Code Playgroud)

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

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

public class BeanRuntimeFactory {

    @Bean
    public RuntimeBean createRuntimeBean() {
        return new RuntimeBean();
    }
}
Run Code Online (Sandbox Code Playgroud)

期待@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

mar*_*bog 6

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

} '
Run Code Online (Sandbox Code Playgroud)


Vad*_*huk 6

看起来我找到了解决方案.因为我使用基于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;
}
Run Code Online (Sandbox Code Playgroud)

而已.

感谢大家.

  • 在不接受构造函数参数之前,自4.1.4起已对其进行了修复。您必须通过调用return runtimeBean(beanName);来记住这一点。您不是直接调用方法runtimeBean,而是在负责在Spring上下文中创建此bean的bean工厂上调用实例化方法,并且参数在传递给实际bean之前由工厂解析。和“ @Configuration”注释,整个过程被ConfigurationClassEnhancer.BeanFactoryAwareMethodInterceptor拦截,它决定如何实例化bean。 (2认同)

Rzv*_*van 6

一个简单的方法:

@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
}
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令来代替 SingletonBeanRegistry:

BeanFactory beanFactory = configContext.getBeanFactory();
Run Code Online (Sandbox Code Playgroud)

无论如何 SingletonBeanBuilder 扩展了 HierarchicalBeanFactory 并且 HierarchicalBeanFactory 扩展了 BeanFactory