Gaofangye

Gaofangye

Manually injecting beans into the Spring IOC container

This functionality is implemented through Spring's built-in ImportBeanDefinitionRegistrar interface.

The steps are as follows:

Create Bean#

 import java.io.Serializable;

 public class User implements Serializable {

     private String username;

     public String getUsername() {
         return username;
     }

     public void setUsername(String username) {
         this.username = username;
     }
 }

Custom MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar interface#

 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
 import org.springframework.core.type.AnnotationMetadata;

 public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
     @Override
     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
         RootBeanDefinition beanDefinition = new RootBeanDefinition(User.class);
         beanDefinition.getPropertyValues().add("username", "I am stored when creating Bean");
         registry.registerBeanDefinition("cn.gaofangye.springbeanstudy.ImportBeanDefinitionRegistrar.demo1.User", beanDefinition);
     }
 }

This piece of code sets the value for the username property in the Bean, and a set method needs to be added for the property.

Custom MyConfig configuration class Import custom Bean creation class#

 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;

 @Configuration
 @Import(MyImportBeanDefinitionRegistrar.class)
 public class MyConfig {
 }

Only in this way can the custom Bean be successfully injected into the Spring IOC container.

Write test class for verification#

 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class Main {

     public static void main(String[] args) {
         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
         User user = applicationContext.getBean(User.class);
         System.out.println(user.getUsername());
     }
 }

Output result#

 11:27:56.094 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e91993f
 11:27:56.103 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 11:27:56.165 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
 11:27:56.165 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
 11:27:56.166 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
 11:27:56.167 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
 11:27:56.170 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myConfig'
 11:27:56.173 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'cn.gaofangye.springbeanstudy.ImportBeanDefinitionRegistrar.demo1.User'
 I am stored when creating Bean

Custom BeanName to get Bean from IOC by BeanName#

 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
 import org.springframework.core.type.AnnotationMetadata;

 public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
     @Override
     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
         RootBeanDefinition beanDefinition = new RootBeanDefinition(User.class);
         beanDefinition.getPropertyValues().add("username", "I am stored when creating Bean");
         registry.registerBeanDefinition("user", beanDefinition);
     }
 }
 registry.registerBeanDefinition("user", beanDefinition);

By using the first parameter of registerBeanDefinition, set the BeanName for the Bean.

Verify getting Bean from IOC by BeanName#

 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class Main {

     public static void main(String[] args) {
         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
         User user = applicationContext.getBean(User.class);
         System.out.println(user.getUsername());

         User findUserByBeanName = (User) applicationContext.getBean("user");
         System.out.println("Bean obtained by BeanName: " + findUserByBeanName.getUsername());
     }
 }

Output result#

 12:30:24.491 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e91993f
 12:30:24.500 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
 12:30:24.560 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
 12:30:24.560 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
 12:30:24.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
 12:30:24.562 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
 12:30:24.565 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'myConfig'
 12:30:24.567 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'user'
 I am stored when creating Bean
 Bean obtained by BeanName: I am stored when creating Bean
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.