Custom BeandefinitionRegistryPostProcessor Registration Bean

Custom BeanDefinitionRegistryPostProcessor

Overview

  • BeanDefinitionRegistryPostProcessor inherits from BeanFactoryPostProcessor, which is a special BeanFactoryPostProcessor. The postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) method defined in BeanDefinitionRegistryPostProcessor allows us to implement custom registered bean definition logic.

use

public class CustomBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor {

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

}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
RootBeanDefinition helloBean = new RootBeanDefinition(Hello.class );
//Add Bean Definition
registry.registerBeanDefinition("hello", helloBean);
}

}
// Method one, initialize via @Bean
@Configuration
public class SpringConfiguration {

@Bean
public CustomBeanDefinitionRegistry customBeanDefinitionRegistry() {
return new CustomBeanDefinitionRegistry ();
}

}
// Method two, initialize via @Component
@Component
public class CustomizeBeanDefinitionRegistryPostProce ssor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
//Print the current stack information
Utils.printTrack(Definitionexecute postProcessBeanRegistry");

//Create a bean definition class object, the bean type is CalculateServiceImpl
RootBeanDefinition helloBean = new RootBeanDefinition(CalculateServiceImpl.class);

//Bean definition registration To the spring environment
beanDefinitionRegistry.registerBeanDefinition("calculateService", helloBean);
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
//Print current stack information
Utils.printTrack("execute postProcessBeanFactory");
}
}

Reference

  • Custom BeanDefinitionRegistryPostProcessor-Stone Perspective-CSDN Blog
  • Spring4.1.8 Extension of Actual Combat Six: Registering Beans to Spring Content (BeanDefinitionRegistryPostProcessor Interface)-Programmer Xin Chen’s Blog-CSDN Blog

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2837 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.