Tag:Dynamic bean

  • Spring dynamic bean registration example sharing

    Time:2020-10-25

    1. In some special scenarios, it is necessary to register beans with spring dynamically2. Spring version 2.5.6 Copy codeThe code is as follows: 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());  } […]