原创

Spring @Import注解详解

1. 简介

@Import注解功能就是和Spring XML 里面 的 <import /> 一样,是用来导入配置类或者一些需要前置加载的类

2. 导入四种类型的类

Import注解源码:

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@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();

}

导入下面四种类(定义在下面):

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

/**
 * @author weilai
 * @email 352342845@qq.com
 * @date 2020/9/4 4:50 下午
 */
@Import({ImportOrdinaryClass.class, ImportConfigurationClass.class, ImportSelectorImplClass.Test.class, ImportBeanDefinitionRegisterImplClass.Test.class})
@Configuration
public class ImportConfig {
}

使用测试:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringDemoApplicationTests {

    @Autowired
    private ImportOrdinaryClass importOrdinaryClass;

    @Autowired
    private ImportConfigurationClass importConfigurationClass;

    @Autowired
    private ImportSelectorImplClass.Test selectImportTest;

    @Autowired
    private ImportBeanDefinitionRegisterImplClass.Test beanDefinitionImportTest;

    @Test
    void testImportOrdinaryClass() {
        importOrdinaryClass.printName();
    }

    @Test
    void testImportConfigurationClass() {
        importConfigurationClass.printName();
    }

    @Test
    void testImportSelectorImplClass() {
        selectImportTest.printName();
    }

    @Test
    void testImportBeanDefinitionRegisterImplClass() {
        beanDefinitionImportTest.printName();
    }

}

2.1 导入普通类

public class ImportOrdinaryClass {

    public void printName() {
        System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
    }
}

2.2 导入@Configuration注解的配置类

import org.springframework.context.annotation.Configuration;

/**
 * @author weilai
 * @email 352342845@qq.com
 * @date 2020/9/4 4:55 下午
 */
@Configuration
public class ImportConfigurationClass {

    public void printName() {
        System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
    }
}

2.3 导入ImportSelector接口的实现类

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author weilai
 * @email 352342845@qq.com
 * @date 2020/9/4 6:10 下午
 */
public class ImportSelectorImplClass implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.weilai.springdemo.ImportSelectorImplClass.Test"};
    }

    static class Test {
        public void printName() {
            System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
        }
    }

}

2.4 导入ImportBeanDefinitionRegistrar接口的实现类

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;

/**
 * @author weilai
 * @email 352342845@qq.com
 * @date 2020/9/4 6:12 下午
 */
public class ImportBeanDefinitionRegisterImplClass implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        registry.registerBeanDefinition("beanDefinitionImportTest", new RootBeanDefinition(Test.class));
    }

    static class Test {

        public void printName() {
            System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
        }
    }
}
正文到此结束
本文目录