spring注解学习之 九 DeferredImportSelector接口

DeferredImportSelector接口

DeferredImportSelector是ImportSelector注解的一种扩展注解。

其执行逻辑是,当实现DeferredImportSelector类重写了

public Class<? extends Group> getImportGroup() 

方法时,则在此类被@Import注解导入时执行getImportGroup方法,如果没有实现getImportGroup方法,则执行

public String[] selectImports(AnnotationMetadata importingClassMetadata)


实现DeferredImportSelector接口的类

package com.personal.spring.selector;

import com.personal.spring.group.MyGroup;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.function.Predicate;

public class MyDeferredImportSelector implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{};
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return null;
    }

    @Override
    public Class<? extends Group> getImportGroup() {
        return MyGroup.class;
    }
}

实现DeferredImportSelector.Group接口的类

package com.personal.spring.group;

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

import java.util.ArrayList;
import java.util.List;

public class MyGroup implements DeferredImportSelector.Group{
    private AnnotationMetadata annotationMetadata;
    @Override
    public void process(AnnotationMetadata metadata, DeferredImportSelector selector) {
        this.annotationMetadata=metadata;
    }

    @Override
    public Iterable<Entry> selectImports() {
        List<Entry> list=new ArrayList<>();
        list.add(new Entry(this.annotationMetadata,"com.personal.spring.bean.Person"));
        return list;
    }
}

配置类

@Configuration
@ComponentScan(value = "com.personal.spring.bean",useDefaultFilters = false
)
@Import({MyImportBeanDefinitionRegistrar.class, MyDeferredImportSelector.class})
public class SpringConfig {
}

主程序

package com.personal.spring.main;

import com.personal.spring.bean.Person;
import com.personal.spring.config.SpringConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();

        Arrays.stream(beanDefinitionNames).forEach(x-> System.out.println(x));

        Person bean = context.getBean(Person.class);
        System.out.println(bean);

    }
}

运行结果

D:programsjdk8injava.exe "-javaagent:D:programsIntelliJ IDEA 2020.3.3libidea_rt.jar=51128:D:programsIntelliJ IDEA 2020.3.3in" -Dfile.encoding=UTF-8 -classpath D:programsjdk8jrelibcharsets.jar;D:programsjdk8jrelibdeploy.jar;D:programsjdk8jrelibextaccess-bridge-64.jar;D:programsjdk8jrelibextcldrdata.jar;D:programsjdk8jrelibextdnsns.jar;D:programsjdk8jrelibextjaccess.jar;D:programsjdk8jrelibextjfxrt.jar;D:programsjdk8jrelibextlocaledata.jar;D:programsjdk8jrelibext
ashorn.jar;D:programsjdk8jrelibextsunec.jar;D:programsjdk8jrelibextsunjce_provider.jar;D:programsjdk8jrelibextsunmscapi.jar;D:programsjdk8jrelibextsunpkcs11.jar;D:programsjdk8jrelibextzipfs.jar;D:programsjdk8jrelibjavaws.jar;D:programsjdk8jrelibjce.jar;D:programsjdk8jrelibjfr.jar;D:programsjdk8jrelibjfxswt.jar;D:programsjdk8jrelibjsse.jar;D:programsjdk8jrelibmanagement-agent.jar;D:programsjdk8jrelibplugin.jar;D:programsjdk8jrelib
esources.jar;D:programsjdk8jrelib
t.jar;D:workspace_ideaspring-pro	argetclasses;C:Userscaopeng.m2
epositoryorgspringframeworkspring-context5.2.8.RELEASEspring-context-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgspringframeworkspring-aop5.2.8.RELEASEspring-aop-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgspringframeworkspring-beans5.2.8.RELEASEspring-beans-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgspringframeworkspring-core5.2.8.RELEASEspring-core-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgspringframeworkspring-jcl5.2.8.RELEASEspring-jcl-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgspringframeworkspring-expression5.2.8.RELEASEspring-expression-5.2.8.RELEASE.jar;C:Userscaopeng.m2
epositoryorgprojectlomboklombok1.18.20lombok-1.18.20.jar com.personal.spring.main.Main
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
springConfig
rainBow
com.personal.spring.bean.Person
com.personal.spring.bean.Person@7bb58ca3

Process finished with exit code 0
原文地址:https://www.cnblogs.com/cplinux/p/15306478.html