Skip to content

ConfigurationClassPostProcessor 之 @Import

java
package com.github.mengweijin.mybatisplus.demo.ImportSelector;

import com.github.mengweijin.mybatisplus.demo.ImportSelector.entity.Phone1;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

@Import({
        Phone1.class,
        Phone2ImportSelector.class,
        Phone3DeferredImportSelector.class,
        Phone4ImportBeanDefinitionRegistrar.class
})
@Component
public class PhoneConfiguration {
}
java
@Data
public class Phone1 {}
@Data
public class Phone2 {}
@Data
public class Phone3 {}
@Data
public class Phone4 {}

ImportSelector

java
@Slf4j
@Data
public class Phone2ImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        log.debug(importingClassMetadata.toString());
        return new String[]{Phone2.class.getName()};
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        log.debug("public Predicate<String> getExclusionFilter()");
        return ImportSelector.super.getExclusionFilter();
    }
}

DeferredImportSelector

java
@Slf4j
@Data
public class Phone3DeferredImportSelector implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        log.debug(importingClassMetadata.toString());
        return new String[]{Phone3.class.getName()};
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        log.debug(" public Predicate<String> getExclusionFilter()");
        return DeferredImportSelector.super.getExclusionFilter();
    }
}

ImportBeanDefinitionRegistrar

java
public class Phone4ImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(Phone4.class).getBeanDefinition();
        registry.registerBeanDefinition(BeanDefinitionReaderUtils.generateBeanName(beanDefinition, registry), beanDefinition);
    }
}

测试

Phone1, Phone2, Phone3, Phone4 的实例通过不同的方式全部被实例化到了 Spring 容器中。

java
package com.github.mengweijin.mybatisplus.demo.ImportSelector;

import com.github.mengweijin.mybatisplus.demo.ImportSelector.entity.Phone1;
import com.github.mengweijin.mybatisplus.demo.ImportSelector.entity.Phone2;
import com.github.mengweijin.mybatisplus.demo.ImportSelector.entity.Phone3;
import com.github.mengweijin.mybatisplus.demo.ImportSelector.entity.Phone4;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@Slf4j
public class TestImportAnnotation {

    @Test
    public void importSelector(){
        String basePackages = "com.github.mengweijin.mybatisplus.demo.ImportSelector";
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(basePackages);

        Assertions.assertNotNull(context.getBean(Phone1.class));
        Assertions.assertNotNull(context.getBean(Phone2.class));
        Assertions.assertNotNull(context.getBean(Phone3.class));
        Assertions.assertNotNull(context.getBean(Phone4.class));

        context.close();
    }
}