preface
Previous events were learned, system initializers and listeners. From here on out, go to the core of the core — bean-related.
At the heart of this section is an exploration of how beans are loaded into the Spring Ioc container.
In this section, the first of the Bean parsing sections, let’s learn what types of Bean registration are available.
Configure beans in N positions
The original is, of course, the way Xml is configured, but since this series is at the heart of Spring Boot, I won’t go into how Xml is configured in the Spring era.
Annotations @ Component
This is the most common way we use it. For classes, inject this class into the Spring Ioc container.
@Component refers to any annotation that contains @Component, such as @Controller, @Service, and so on.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(annotation = Component.class)
String value(a) default "";
}
Copy the code
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(annotation = Component.class)
String value(a) default "";
}
Copy the code
As you can see, @Controller and @Service annotations include @Component.
Annotations @ Bean
That’s the way we do it. Used on methods to register the return value of the method as a Bean.
Both of these are fairly common ways, so I’m not going to do a demonstration.
Realize the FactoryBean
-
Abstract interface MyBean
public interface MyBean { String getName(a); } Copy the code
-
The implementation class MyOneBean
public class MyOneBean implements MyBean{ @Override public String getName(a) { return "MyOneBean"; }}Copy the code
-
MyFactoryBean
Note that the @Component blessing is also required here.
@Component public class MyFactoryBean implements FactoryBean<MyBean> { @Override public MyBean getObject(a) throws Exception { return new MyOneBean(); } @Override publicClass<? > getObjectType() {returnMyBean.class; }}Copy the code
-
test
@Test public void one(a) { System.out.println(myBean.getName()); } // MyOneBean Copy the code
Implement BeanDefinitionRegistryPostProcessor
-
MyTwoBean
public class MyTwoBean implements MyBean{ @Override public String getName(a) { return "MyTwoBean"; }}Copy the code
-
MyBeanDefinitionRegistryPostProcessor
@Component public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(); rootBeanDefinition.setBeanClass(MyTwoBean.class); beanDefinitionRegistry.registerBeanDefinition("myTwoBean",rootBeanDefinition); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {}}Copy the code
-
test
@Testpublic void one(a) { System.out.println(myBean.getName()); }// MyTwoBean Copy the code
Implement ImportBeanDefinitionRegistrar
-
MyThreeBean
public class MyThreeBean implements MyBean{ @Override public String getName(a) { return "myThreeBean"; }} Copy the code
-
MyImportBeanDefinitionRegistrar
Note that there is no @Component annotation
public class MyImportBeanDefinitionRefistart implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(); rootBeanDefinition.setBeanClass(MyThreeBean.class); registry.registerBeanDefinition("myThreeBean",rootBeanDefinition); }} Copy the code
-
Start the class add @ Import (MyImportBeanDefinitionRefistart. Class)
-
test
@Testpublic void one(a) { System.out.println(myBean.getName()); }// myThreeBean Copy the code
conclusion
In this section, you learned about several ways to register beans.
This is to pave the way for later Bean loading.