Springboot之手动注册代码

代码注册第一步 实现 接口类 BeanDefinitionRegistryPostProcessor

第二步骤 实现方法:
postProcessBeanDefinitionRegistry

第三步骤 实现方法:
BeanDefinitionRegistry

第四步实现Bean工厂方法
postProcessBeanFactory

完整方法:

package com.jeesite.modules;
/**

  • @ClassName:MultiBeanDefinitionRegistryPostProcessor
  • @author: 短笛吟风
  • @description:TODO
  • @createDate: 2019/12/17
  • @version: 1.0

*/
@Configuration
public class MultiBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

private static final Logger logger = LoggerFactory.getLogger(MultiBeanDefinitionRegistryPostProcessor.class);

private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
@Autowired
ApplicationContext applicationContext;


@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {



    registerBean(registry,"sqlTemplateRegisterBeanMessagecenter",SqlTemplateRegisterBean.class);




}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {


    try {


        DomainBinding domainBinding=new DomainBinding();
        domainBinding.setDomainPackages("com.jeesite.modules.sale.entity");
        com.yingmoo.dbframe.script.MysqlBuilder mysqlBuilder=new MysqlBuilder();
        domainBinding.setSqlBuilder(mysqlBuilder);
        SqlSessionFactory sqlSessionFactoryBean=beanFactory.getBean(SqlSessionFactory.class);
        domainBinding.setSqlSessionFactory(sqlSessionFactoryBean);
        com.yingmoo.dbframe.script.MysqlBuilder mysqlBuilder1=new MysqlBuilder();
        domainBinding.setSqlBuilder(mysqlBuilder);
        List domainBindingList=new ArrayList<>();
        domainBindingList.add(domainBinding);
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("sqlTemplateRegisterBeanMessagecenter");
        MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
        mutablePropertyValues.add("domainBingings",domainBindingList);



    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void registerBean(BeanDefinitionRegistry registry, String name, Class beanClass) {


    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);

}

}

你可能感兴趣的:(spring)