国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

spring動態bean注冊示例分享

2024-09-05 00:22:04
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了spring動態bean注冊示例,需要的朋友可以參考下

1.在一些特殊的場景中需要動態向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 根據類名查找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);
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东平县| 铁岭市| 绩溪县| 武宣县| 灯塔市| 韩城市| 松滋市| 射阳县| 溆浦县| 平谷区| 新昌县| 漾濞| 崇明县| 广饶县| 鹤峰县| 巴青县| 赤城县| 西盟| 新乡市| 新野县| 丰镇市| 旺苍县| 眉山市| 庆安县| 岳普湖县| 浦城县| 高邑县| 兴宁市| 鲜城| 土默特左旗| 盐源县| 宜兴市| 新巴尔虎左旗| 尼玛县| 益阳市| 津市市| 商都县| 如皋市| 阳江市| 青龙| 乌审旗|