分享好友 编程语言首页 频道列表

spring动态bean注册示例分享

JSP编程  2015-06-26 10:470

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

查看更多关于【JSP编程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Springboot + mybatis + React+redux+React-router+antd+Typescript(二): React+Typescrip项目的搭建
前言:            后台搭建完以后开始搭建前端,使用create-react-app搭建项目非常方便。           前端主要是如何向后台请求数据,以及如何使用redux管理state,路由的配置.           前端github地址:  https://github.com

0评论2023-02-09413

微信小程序 springboot nginx 做图片存储 上传 浏览
微信小程序前端-springboot后端-nginx图片存储前言本人小白一名,这是第一次学习微信小程序,特此做个记录。首先准备nginx做图片存储选择一个地址存放图片#我的地址[root@VM_0_16_centos images]# pwd/home/photos/images[root@VM_0_16_centos images]#然后配

0评论2023-02-09533

Ruby中文乱码问题 springmvc中文乱码
中文乱码问题解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*-(EMAC写法) 或者 #coding=utf-8 就行了。源代码文件中,若包含中文编码,则需要注意两点:1. 必须在首行添加 # -*- coding: UTF-8 -*-,告诉解释器使用utf-8来解析源码。2. 必须设置编

0评论2023-02-09738

SpringBoot + nodeJS + zookeeper 搭建微服务示例
总体来说该项目由服务注册 + 服务发现 + 服务代理 + 服务调用四部分组成。使用java客户的开发服务注册组件,它是整个微服务架构中的服务注册表,使用Node.js客户端开发服务发现组件,它用于在服务注册表中根据具体的服务名称获取对应的服务配置。  由项目1

0评论2023-02-09398

Spring注解与P/C命名空间超详细解析
目录注解实现自动装配@Autowire注解@Qualifier注解@Resource注解@Component@Scope@ComponentScan@Bean@Configuration@ValueP命名空间注入C命名空间注入Spring开发包名解释Java方式配置注解实现自动装配@Autowire注解@Autowire注解,自动装配通过类型,名字如

0评论2023-02-09315

SpringBoot使用过滤器、拦截器和监听器的案例代码(Springboot搭建java项目)
目录SpringBoot使用过滤器、拦截器和监听器一、SpringBoot使用过滤器Spring boot过滤器的使用(两种方式)方式一:方式二:二、SpringBoot使用拦截器三、过滤器和拦截器的执行顺序四、SpringBoot使用监听器1、统计网站最多在线人数监听器的例子2、springboot

0评论2023-02-09751

spring-data-redis 自定义缓存(@annotation+lua+aop)(一)
介绍:使用spring-data-redis 框架 ,利用@annotation和redis lua,spring aop 实现数据的 save、list、update、delete对缓存的影响操作(考虑增加异步数据)  save:数据库数据添加、缓存数据添加、list后的缓存数据添加  list:缓存数据增加  update:数

0评论2023-02-08436

SpringMVC文件上传多文件上传实例
这篇文章主要介绍了SpringMVC文件上传 多文件上传实例,有需要的朋友可以参考一下

0评论2015-07-16114

spring实现jdbctemplate添加事务支持示例
这篇文章主要介绍了spring实现jdbctemplate添加事务支持示例,重写JdbcTemplate增加beginTranstaion,commit,rollback方法,需要的朋友可以参考下

0评论2015-06-26148

通过spring用beanshell实现java接口示例
这篇文章主要介绍了通过spring用beanshell实现java接口示例,需要的朋友可以参考下

0评论2015-06-26105

更多推荐