The two methods allow automatic assembly bean container in a previous blog:
1, assembled using annotations @Bean
2, be coated using @ComponentScan scan type scanning with @ Controller, @ Service, @ Repository, @ Component annotation can be loaded bean
Another way to use annotations were @Import use when referring to third-party classes
usage:
First, add @Import comment directly on the main configuration class, to create the value assigned to the type of annotation
First, create a class to load external incoming
public class Color { ... }
Such can be seen not added any package that allows scanned notes, created in the container is not loaded into the container
@Import then add annotations in the main configuration class
@Configuration @Import ({Color. Class }) // annotation type is required inside the IOC loaded class public class MainConfig2 { }
Test class and return the results:
public class IOCTest { @Test public void test4() { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class); String[] names = applicationContext.getBeanDefinitionNames();//返回容器中所有bean的名字 for (String name : names) { System.out.println(name); } } }
operation result:
mainConfig2
com.wxj.bean.Color
Observation available, default import component uses @Import ioc container in full class name id
Second, the realization ImportSelector Interface
1, create a method to implement a ImportSelector Interface ()
public class MyImportSelector the implements ImportSelector { / ** * AnnotationMetadata class name can be obtained notes, annotations and other information * * Full class name of the returned String array is to make @Import notes loaded class * / public String [] selectImports (AnnotationMetadata importingClassMetadata ) { return new new String [] { "com.wxj.bean.Color"}; // needs to manage ioc container full class name, may be formed of a plurality of classes, separated } }
2, change the value of the main values @Import annotation in the configuration class
@Configuration @Import({MyImportSelector.class}) public class MainConfig2 { }
3, run again
mainConfig2
com.wxj.bean.Color
Third, to achieve ImportBeanDefinitionRegistrar interface (custom hand the bean)
1, to achieve ImportBeanDefinitionRegistrar, code is as follows:
public class MyImportBeanDefinitionRegistrar the implements ImportBeanDefinitionRegistrar { / ** * importingClassMetadata: Direct current class information * registry: Register bean class definition, by using it to register as a bean container * require all added to the vessel by a bean manual registration, call BeanDefinitionRegistry the manual registration method registerBeanDefinition * * / public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry Registry) { // definition information specifying the bean ... RootBeanDefinition BeanDefinition = new new RootBeanDefinition (Color. class ); // to a bean from the container id defined registry.registerBeanDefinition("Color", beanDefinition); } }
2, the main configuration change class
@Configuration @Import({MyImportBeanDefinitionRegistrar.class}) public class MainConfig2 { }
3, operating results
mainConfig2
Color
Note: When reading the source code SpringBoot later, the second method is extremely broad application