最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年03月06日 09:30:10   作者:  
這篇文章主要介紹了spring動(dòng)態(tài)bean注冊示例,需要的朋友可以參考下

1.在一些特殊的場景中需要?jiǎng)討B(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);
  }
 }
}

相關(guān)文章

最新評論

河津市| 和静县| 嵊州市| 合水县| 凤城市| 阿鲁科尔沁旗| 龙陵县| 科尔| 青海省| 陇川县| 甘泉县| 武川县| 无为县| 永嘉县| 石林| 赫章县| 黄浦区| 靖边县| 平远县| 朝阳区| 营山县| 左云县| 绥中县| 肥西县| 英山县| 长宁县| 盐源县| 江陵县| 垦利县| 松滋市| 新龙县| 福清市| 新乡市| 烟台市| 克拉玛依市| 黄山市| 周宁县| 囊谦县| 巴中市| 阿城市| 泾川县|