Ang, I haven’t written an article for a long time. I don’t know where to start… Remember before learning spring source code are almost forgotten, the follow-up to write a spring source code article. Today we’ll start with the factory back processor, and subsequent articles will show how Spring uses the factory back processor to scan beans.

In fact, I always like to read short articles, because they are too long to read, and then I lose interest… I do not know whether we are also like this? So I decided to write something short and not pithy

Those interested in Spring’s source code should take a look.

1.BeanFactoryPostProcessor

This is the top-level interface of spring’s factory backloader, which provides only one method. Take a look at this interface.

public interface BeanFactoryPostProcessor {

   /**
    * Modify the application context's internal bean factory after its standard
    * initialization. All bean definitions will have been loaded, but no beans
    * will have been instantiated yet. This allows for overriding or adding
    * properties even to eager-initializing beans.
    * @param beanFactory the bean factory used by the application context
    * @throws org.springframework.beans.BeansException in case of errors
    */
   void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}
Copy the code

Provide a bean factory where all beans are loaded but not instantiated, so you can do whatever you want before instantiating the bean.

Here’s an example:

Here is a car class, add its:

@Component
public class Car {
   private String name;
   private Double price;

   public void setName(String name) {
      this.name = name;
   }

   public void setPrice(Double price) {
      this.price = price;
   }

   @Override
   public String toString(a) {
      return "Car{" +
            "name='" + name + '\' ' +
            ", price=" + price +
            '} '; }}Copy the code

Then customize a factory post-processor:

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
      BeanDefinition benzCarBeanDefinition = beanFactory.getBeanDefinition("car");
      / / property
      MutablePropertyValues propertyValues = benzCarBeanDefinition.getPropertyValues();
      // The set method in the bean is called
      propertyValues.add("name"."Mercedes c200l");
      propertyValues.add("price".300000.00); }}Copy the code

We need to write a configuration class that defines the packages to be scanned:

@Configuration
@ComponentScan("test")
public class MyConfiguration {}Copy the code

Then write a test class:

public class Test {

   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new 			    AnnotationConfigApplicationContext(MyConfiguration.class);
      Car car = (Car) context.getBean("car");
      System.out.println(car);
     Car{name=' Mercedes-Benz c200L ', price=300000.0}}}Copy the code

In the final output, the name and price of car are assigned values.

2.BeanDefinitionRegistryPostProcessor

This interface inherits BeanFactoryPostProcessor and is an extension of BeanFactoryPostProcessor.

public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

   /**
    * Modify the application context's internal bean definition registry after its
    * standard initialization. All regular bean definitions will have been loaded,
    * but no beans will have been instantiated yet. This allows for adding further
    * bean definitions before the next post-processing phase kicks in.
    * @param registry the bean definition registry used by the application context
    * @throws org.springframework.beans.BeansException in case of errors
    */
   void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}
Copy the code

The method of this interface provides a parameter to Registry, which is a registry that you can use to do some registration and dynamically de-register beans.

Here’s another chestnut:

A custom BeanDefinitionRegistryPostProcessor:

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
   /** * extended method: provides a registry to dynamically de-register beans *@param registry the bean definition registry used by the application context
    * @throws BeansException
    */
   @Override
   public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
      for (int i = 0; i < 10; i++) {
         BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Car.class);
         builder.addPropertyValue("name"."Mercedes _" + i);
         registry.registerBeanDefinition("car" + i, builder.getBeanDefinition());
      }
      System.out.println("Dynamic injection bean complete");
   }

   /** * the method #postProcessBeanFactory in the parent class can modify and set the bean properties *@param beanFactory the bean factory used by the application context
    * @throws BeansException
    */
   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}}Copy the code

Then write a test class to see if 10 Cars have been injected into the Spring container.

public class Test {

   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
      Map<String, Car> beansOfType = context.getBeansOfType(Car.class);
      beansOfType.forEach((k,v)->{
         System.out.println(k+"> > >"+v.toString()); }); }}Copy the code

Finally, there are 10 instances of CAR beans in the Spring container.

This article briefly introduces spring’s factory post-processor, because spring uses the factory post-processor to scan the whole Spring project, the bean analysis, some annotation processing, I feel that the factory post-processor is spring source code reading must know something, more important.

In the next article, we will talk about how Spring can scan the whole project using the factory rear processor. If you are interested, you can follow it or search “shanzhu” on wechat to read the latest article.