[英]Spring register bean in runtime
I would like to register spring bean in runtime, so i built that class: 我想在运行时注册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?
}
}
is this cast always safe ? 这个演员总是安全吗?
At the moment (Spring 4.1) there is only implementation of ApplicationContext
that does not implement the ConfigurableApplicationContext
interface: that is org.springframework.test.web.servlet.setup.StubWebApplicationContext
from spring-test
目前(春季4.1)只有
ApplicationContext
实现未实现ConfigurableApplicationContext
接口:即spring-test
org.springframework.test.web.servlet.setup.StubWebApplicationContext
So I think you are relative save and need to pay some attention to the tests. 所以我认为您是相对省钱的,需要对测试有所注意。
But just because there is no meaningful implementation at the moment, it does not mean that there is no change in future version, or somebody/some Framework has implemented it already! 但是仅仅因为目前没有有意义的实现,并不意味着将来的版本没有任何变化,或者某人/某些Framework已经实现了它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.