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

首頁 > 編程 > JSP > 正文

spring動態(tài)bean注冊示例分享

2020-07-27 21:34:33
字體:
供稿:網(wǎng)友

1.在一些特殊的場景中需要動態(tài)向spring注冊bean
2.spring版本2.5.6

復(fù)制代碼 代碼如下:

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);
  }
 }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 佳木斯市| 昭苏县| 贵州省| 都江堰市| 武隆县| 太康县| 辽源市| 邵阳市| 长子县| 攀枝花市| 新竹县| 昌宁县| 中宁县| 泸水县| 陆河县| 哈尔滨市| 襄城县| 高淳县| 玛曲县| 阿勒泰市| 娱乐| 休宁县| 通化市| 金湖县| 临城县| 宜都市| 嘉义县| 万全县| 左云县| 修水县| 汉沽区| 吴桥县| 东乡| 伊吾县| 建瓯市| 安西县| 闻喜县| 甘南县| 普宁市| 曲沃县| 新源县|