[英]Spring register bean in runtime
我想在运行时注册spring bean,所以我建立了这个类:
public class SpringBeanRegister implements BeanRegister, ApplicationContextAware {
private ConfigurableApplicationContext context;
public void registerBean(Object object) {
ConfigurableListableBeanFactory factory = context.getBeanFactory();
factory.registerSingleton("beanName", object);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = (ConfigurableApplicationContext) applicationContext; // -- safe?
}
}
这个演员总是安全吗?
目前(春季4.1)只有ApplicationContext
实现未实现ConfigurableApplicationContext
接口:即spring-test
org.springframework.test.web.servlet.setup.StubWebApplicationContext
所以我认为您是相对省钱的,需要对测试有所注意。
但是仅仅因为目前没有有意义的实现,并不意味着将来的版本没有任何变化,或者某人/某些Framework已经实现了它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.