To introduce the use of the @import annotation, let’s look at the source code:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

   / * * * {@link Configuration @Configuration}, {@link ImportSelector},
    * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
    */Class<? >[] value(); }Copy the code

From the comments, we can see the @ Import annotations can be @ the Configuration, ImportSelector, ImportBeanDefinitionRegistrar and ordinary Import component class.

Preparing the Spring Environment

The Spring depends on

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.3.7</version>
</dependency>
Copy the code

Import @ the Configuration class

Bean to import: Hello

public class Hello {

    public void print(a) {
        System.out.println("hello word"); }}Copy the code

Configuration class: HelloConfiguration

@Configuration
public class HelloConfiguration {

    @Bean
    public Hello createHello(a) {
        return newHello(); }}Copy the code

Launch class: SpringTestApplication

@Import(HelloConfiguration.class)
public class SpringTestApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = newAnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); }}Copy the code

After startup, the console output is as follows:

hello word

Process finished with exit code 0
Copy the code

Indicates that the Hello object is indeed injected into the container via the @import annotation.

Import ImportSelector

ImportSelector implementation class: HelloImportSelector

ImportSelector is an interface that requires overriding the selectImports method to implement.

The selectImports method returns an array of strings. The elements in this array are the fully qualified names of the classes that need to be imported into the container.

Now we implement the ImportSelector interface and rewrite the selectImports method to return the fully qualified name of the Hello class.

public class HelloImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        List<String> classNameList = new ArrayList<>();
        classNameList.add("com.xgc.entity.Hello");
        returnStringUtils.toStringArray(classNameList); }}Copy the code

Launch class: SpringTestApplication

@Import(HelloImportSelector.class)
public class SpringTestApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = newAnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); }}Copy the code

After startup, the console output is as follows:

hello word

Process finished with exit code 0
Copy the code

Import ImportBeanDefinitionRegistrar

ImportBeanDefinitionRegistrar implementation class: HelloImportDefinitionRegistrar

public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Hello.class); registry.registerBeanDefinition("hello", rootBeanDefinition); }}Copy the code

Launch class: SpringTestApplication

@Import(HelloImportBeanDefinitionRegistrar.class)
public class SpringTestApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = newAnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); }}Copy the code

After startup, the console output is as follows:

hello word

Process finished with exit code 0
Copy the code

Import beans directly

Launch class: SpringTestApplication

@Import(Hello.class)
public class SpringTestApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = newAnnotationConfigApplicationContext(SpringTestApplication.class); Hello hello = applicationContext.getBean(Hello.class); hello.print(); }}Copy the code

After startup, the console output is as follows:

hello word

Process finished with exit code 0
Copy the code