spring動(dòng)態(tài)bean注冊(cè)示例分享
1.在一些特殊的場(chǎng)景中需要?jiǎng)討B(tài)向spring注冊(cè)bean
2.spring版本2.5.6
public class ServiceServiceImpl implements ServiceService, ApplicationContextAware {
@Override
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
public void addBeanService(Service service) throws BVSException {
if (!context.containsBean(service.getServiceName())) {
Class<?> serviceClass = getServiceClass(service.getClassName());
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(serviceClass);
beanDefinitionBuilder.addPropertyValue("servicename", service.getServiceName());
registerBean(service.getServiceName(), beanDefinitionBuilder.getRawBeanDefinition());
}
}
/**
* @desc 向spring容器注冊(cè)bean
* @param beanName
* @param beanDefinition
*/
private void registerBean(String beanName, BeanDefinition beanDefinition) {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context;
BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext
.getBeanFactory();
beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
}
/**
* @desc 根據(jù)類名查找class
* @param className
* @return
* @throws BVSException
*/
private Class<?> getServiceClass(String className) throws BVSException {
try {
return Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
log.error("not found service class:" + className, e);
throw new BVSException("not found service class:" + className, e);
}
}
}
相關(guān)文章
JSP用過(guò)濾器解決request getParameter中文亂碼問(wèn)題
在服務(wù)器端用request.getParameter()讀取參數(shù)時(shí),很容易出現(xiàn)中文亂碼現(xiàn)象,下面是JSP用過(guò)濾器解決request中文亂碼問(wèn)題的具體實(shí)現(xiàn)2014-09-09jsp使用ECharts動(dòng)態(tài)在地圖上標(biāo)識(shí)點(diǎn)
echarts地圖展示功能很強(qiáng)大,官網(wǎng)上靜態(tài)展示的例子很多了,動(dòng)態(tài)的資料少,需要參考本文的可以進(jìn)來(lái)了解一下。2016-10-10jsp利用POI生成Excel并在頁(yè)面中導(dǎo)出的示例
本篇文章主要是介紹jsp利用POI生成Excel并在頁(yè)面中導(dǎo)出的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10JSP使用Servlet過(guò)濾器進(jìn)行身份驗(yàn)證的方法
這篇文章主要介紹了JSP使用Servlet過(guò)濾器進(jìn)行身份驗(yàn)證的方法,結(jié)合實(shí)例形式分析了Servlet過(guò)濾器的實(shí)現(xiàn)方法及jsp身份驗(yàn)證的具體使用技巧,需要的朋友可以參考下2015-12-12