spring動(dòng)態(tài)bean注冊示例分享
1.在一些特殊的場景中需要?jiǎng)討B(tài)向spring注冊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容器注冊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)文章
通過spring用beanshell實(shí)現(xiàn)java接口示例
這篇文章主要介紹了通過spring用beanshell實(shí)現(xiàn)java接口示例,需要的朋友可以參考下2014-03-03
jsp頁面數(shù)據(jù)分頁模仿百度分頁效果(實(shí)例講解)
下面小編就為大家?guī)硪黄猨sp頁面數(shù)據(jù)分頁模仿百度分頁效果(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
web開發(fā)之對比時(shí)間大小的工具函數(shù)的實(shí)例詳解
這篇文章主要介紹了web開發(fā)之對比時(shí)間大小的工具函數(shù)的實(shí)例詳解的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下2017-08-08
如何在Jsp中使用JDBC來聯(lián)結(jié)MySql
如何在Jsp中使用JDBC來聯(lián)結(jié)MySql...2006-10-10
jsp項(xiàng)目中更改tomcat的默認(rèn)index.jsp訪問路徑的方法
如何更改tomcat的默認(rèn)index.jsp訪問路徑,jsp的工程下有一個(gè)叫做WEB-INF文件夾下的web.xml打開它,按照下面的方法即可修改2013-11-11

