栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中@Import的各种用法以及ImportAware接口详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中@Import的各种用法以及ImportAware接口详解

@import 注解

@import注解提供了和XML中元素等价的功能,实现导入的一个或多个配置类。@import即可以在类上使用,也可以作为元注解使用。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface import {

  
  Class[] value();

}

注解中只有一个value();。支持导入@Configuration标注的配置类,实现importSelector接口的类、实现importBeanDefinitionRegistrar接口的类和普通的@component类。

作为元注解使用

@import可以作为元注解使用,可以在@import的继承上封装一层。我的理解是,这样做不会对外(使用方)暴露我内部的具体实现细节。

举个例子:例如@EnableAspectJAutoProxy注解。

@import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {

@EnableAspectJAutoProxy就是被@import这个元注解所标志了,我们(程序员)通过使用@EnableAspectJAutoProxy来开启AspectJAutoProxy,而Spring底层是通过@import导入相应的配置类来实现的。

导入实现importSelector接口的类

先来看一下importSelector接口,该接口中只有一个方法:

public interface importSelector {
  String[] selectimports(Annotationmetadata importingClassmetadata);
}

importSelector,输入选择器。该接口就是用来根据给定的条件,选择导入哪些配置类。

举个例子:例如@EnableTransactionManagement注解。

@import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {

在@EnableTransactionManagement注解中使用了@import(TransactionManagementConfigurationSelector.class)注解,其中TransactionManagementConfigurationSelector类就是实现了importSelector接口。

public class TransactionManagementConfigurationSelector extends AdviceModeimportSelector {
  @Override
  protected String[] selectimports(AdviceMode adviceMode) {
    switch (adviceMode) {
      case PROXY:
 return new String[] {AutoProxyRegistrar.class.getName(),
     ProxyTransactionManagementConfiguration.class.getName()};
      case ASPECTJ:
 return new String[] {
     TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
      default:
 return null;
    }
  }
}

方法的内部实现逻辑也很简单,就是根据不同的AdviceMode导入不同的配置类,来实现事务管理。

导入实现importBeanDefinitionRegistrar接口的类

importBeanDefinitionRegistrar接口中也只有一个方法:

public interface importBeanDefinitionRegistrar {
  void registerBeanDefinitions(Annotationmetadata importingClassmetadata, BeanDefinitionRegistry registry);
}

该接口允许我们根据所给的注解元数据,按需注册额外的BeanDefinition。

举个例子:例如@EnableAspectJAutoProxy注解。

@import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {

@EnableAspectJAutoProxy注解引入了AspectJAutoProxyRegistrar.class类,这个类就是实现了importBeanDefinitionRegistrar接口。

class AspectJAutoProxyRegistrar implements importBeanDefinitionRegistrar {

  @Override
  public void registerBeanDefinitions(
      Annotationmetadata importingClassmetadata, BeanDefinitionRegistry registry) {

    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);
      }
    }
  }
}

registerBeanDefinitions中调用了AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);方法,这个方法就是在往传入的BeanDefinitionRegistry registry中注册BeanDefinition。注册了BeanDefinition之后,Spring就会去实例化这个Bean,从而达到AspectJAutoProxy作用。

导入@Configuration类

这次@import最常见是使用方法。我们可以拆分配置类,然后在程序中按需导入相应的配置。

举个例子:例如@EnableRetry注解。使用这个注解可以开启retry功能。

@EnableAspectJAutoProxy(proxyTargetClass = false)
@import(RetryConfiguration.class)
public @interface EnableRetry {

其内部就是导入了RetryConfiguration这个配置类。

importAware接口

importAware接口是需要和@import一起使用的。在@import作为元注解使用时,通过@import导入的配置类如果实现了importAware接口就可以获取到导入该配置类接口的数据配置。有点绕,我们直接上代码。

举个例子:@EnableAsync注解。

@import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
//AsyncConfigurationSelector源码
public class AsyncConfigurationSelector extends AdviceModeimportSelector {

  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;
    }
  }
}

默认情况下使用AdviceMode为PROXY,导入了ProxyAsyncConfiguration类。

@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {

  @Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
    Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
    AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
    Class customAsyncAnnotation = this.enableAsync.getClass("annotation");
    if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) {
      bpp.setAsyncAnnotationType(customAsyncAnnotation);
    }
    if (this.executor != null) {
      bpp.setExecutor(this.executor);
    }
    if (this.exceptionHandler != null) {
      bpp.setExceptionHandler(this.exceptionHandler);
    }
    bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass"));
    bpp.setOrder(this.enableAsync.getNumber("order"));
    return bpp;
  }
}

在ProxyAsyncConfiguration的asyncAdvisor方法中需要获取到@EnableAsync上的一些设置值,例如:this.enableAsync.getBoolean("proxyTargetClass"),this.enableAsync.getNumber("order")。

this.enableAsync是其父类AbstractAsyncConfiguration的属性。AbstractAsyncConfiguration实现了importAware接口,从而就可以获取到@EnableAsync上的信息了。

// AbstractAsyncConfiguration#setimportmetadata 源码
public void setimportmetadata(Annotationmetadata importmetadata) {
  this.enableAsync = AnnotationAttributes.fromMap(
      importmetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
  if (this.enableAsync == null) {
    throw new IllegalArgumentException(
 "@EnableAsync is not present on importing class " + importmetadata.getClassName());
  }
}

可能这个例子有点复杂的,还有一个稍微简单一点的例子:EnableRedisHttpSession。关于这个,读者可以自己去看一下源码debug学习一下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号