Spring Boot自動配置の@Enbale*と@Importコメント

4412 ワード

リンクの説明を追加し、リンクの説明を追加します。
Spring Bootの自動配置はこのように強大で、例えば私達がよく使う@Enbale*注釈はある方面に対する支持を開けます。では、@Enbale*の注釈の原理は何ですか?一、@Enbale*注釈と@Import注釈との関係@Enbale*例:
@EnbleSchedulingオープン計画タスクのサポート@EnbleAsync非同期方法のサポート@EnbleAsppectJAtoxyオープンAspect Jエージェントへのサポート@EnbaleTransation Managementオープン事務へのサポート@EnbaleCachingオープン注式キャッシュへのサポート@
これらの@Enbale*のソースコードを見てみますと、@Enable*のコメントはすべて@Importの組み合わせです。@Enbale*自動オープンの実現は、Spring Boot Reference Guideの原文You need not put all your@Configration into a single classを導入しました。The@Import annotation can be used to importditional configration clases.
すべての@Configrationを一つのクラスに置く必要はありません。Importコメントは追加の構成クラスを導入することができます。
@Importコメントの主な機能は、追加の構成情報を導入することです。
  • Provides functionlity equivalent to the{code}element in Spring XML.
  • Allows for importing{@code@Configration}clases、{@link Import Selector}and
  • {@link ImportBeanDefinitionRegistration}implements,as well as reglar component
  • class(as of 4.2;anlogous to@@link AnnotationConfigAplication Controntatext).
  • 以下の3つの使用方法1、直接配置類(@Configration類)@Target@RetentType@Retension@Retension Policy.RUNTIME)@Import@Dcumented putblic@interface able@
    )
    EnbaleSchenduling注釈が直接配置類Schenduling Configrationを導入するのを見ることができます。この類は@Configrationを注釈しました。そしてscheduledAnnotationProcessorのBeanを登録しました。Schduling Configrationのソースは下記の通りです。
    @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
        return new ScheduledAnnotationBeanPostProcessor();
    }
    
    )
    2、条件に応じて配置類を選択する(Import Selectorインターフェースを実現する)どの配置類を導入するかが確定されていない場合は、@Importによって識別されたクラスまたは他の注釈(通常は注釈)の定義情報に基づいて配置類を選択する必要があります。Import Selectorインターフェースは一つの方法だけString[]selectImports;
    AnnotationMetadata:現在の構成クラスの注釈を得るための例:@Target@Retension Policy.RUNTIME)@Dcumented@Import(AyncConfigrationSelectore.class)putblic@interface Ence
    Class extends Annotation> annotation() default Annotation.class;
    
    boolean proxyTargetClass() default false;
    
    AdviceMode mode() default AdviceMode.PROXY;
    
    int order() default Ordered.LOWEST_PRECEDENCE;
    
    )
    AdyncConfigrationSelectorはAdviice ModeImportSelectorを継承し、Advice ModeImportSelector類はImportSelectorインターフェースを実現します。Advice Modeによって生明きの異なるBen public clast AyncConfigrationSector Adlector
    private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
            "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
    
    @Override
    @Nullable
    public String[] selectImports(AdviceMode adviceMode) {
        switch (adviceMode) {
            case PROXY:
                return new String[] {ProxyAsyncConfiguration.class.getName()};
            case ASPECTJ:
                return new String[] {ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME};
            default:
                return null;
        }
    }
    
    )
    3、動的登録ビーン(ImportBenDefinitionRegistarインターフェースを実現する)は、一般的にユーザーがどのビーンが容器に入れるべきかを正確に知っていれば、自分はspringから提供された注釈で識別すればいいです。例えば@Component、@Service、@Repository、@Benなどです。不確定なクラスやspring専用ではないので、springの注釈で侵入的なマークをしたくないです。@Importで注釈して、ImportBenDefinitionRegistarインターフェースを実現して、動的にビーンを登録します。例えば@Target@Docmented@Import(AsppectJAxyRegistral.css)putblic@interface EnPolicy.RUNTIME)@Docmented@Import@
    boolean proxyTargetClass() default false;
    
    boolean exposeProxy() default false;
    
    )
    AsppectJAutoxyRegistarはImportBeanDefinitionRegistarインターフェースを実現しました。Import BeabinDefinitionRegistarの役割は、運転時にビーンを自動的に既存の配置類に追加し、書き換え方法:public void register BenDefinitions
    AnnotationMetadataパラメータを使用して、現在の構成クラスの注釈BeanDefinitionRegistryパラメータを取得してビーンを登録します。
    ソースコード:@Override public void register BeanDefinitions(AnnotationMetadata importingClass Metadata,BeanDefinitionRegistry)
    AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
    
    AnnotationAttributes enableAspectJAutoProxy =
            AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
    if (enableAspectJAutoProxy != null) {
        if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
        }
        if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
            AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
        }
    }
    
    )
    Mybatisで有名な@MapperScanもこの三、公式文書の公式文書です。