浅谈Spring中@Import注解的作用和使用

 更新时间:2020年05月28日 11:41:50   作者:潘超博客  
这篇文章主要介绍了浅谈Spring中@Import注解的作用和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

@Import用来导入@Configuration注解的配置类、声明@Bean注解的bean方法、导入ImportSelector的实现类或导入ImportBeanDefinitionRegistrar的实现类。

@Import注解的作用

查看Import注解源码

/**
 * Indicates one or more {@link Configuration @Configuration} classes to import.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Only supported for classes annotated with {@code @Configuration} or declaring at least
 * one {@link Bean @Bean} method, as well as {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations.
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes
 * should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit,
 * IDE-friendly navigation between {@code @Configuration} class methods.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use {@link ImportResource @ImportResource}
 *
 * @author Chris Beams
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

 /**
 * The @{@link Configuration}, {@link ImportSelector} and/or
 * {@link ImportBeanDefinitionRegistrar} classes to import.
 */
 Class<?>[] value();
}

分析类注释得出结论:

  • 声明一个bean
  • 导入@Configuration注解的配置类
  • 导入ImportSelector的实现类
  • 导入ImportBeanDefinitionRegistrar的实现类

@Import注解的使用

声明一个bean

package com.example.demo.bean;

public class TestBean1 {
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class})
@Configuration
public class AppConfig {
}

导入@Configuration注解的配置类

package com.example.demo.bean;

public class TestBean2 {
}

package com.example.demo.bean;

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

@Configuration
public class TestConfig {
  @Bean
  public TestBean2 getTestBean2(){
    return new TestBean2();
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class})
@Configuration
public class AppConfig {
}

导入ImportSelector的实现类

package com.example.demo.bean;

public class TestBean3 {
}

package com.example.demo.bean;

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

public class TestImportSelector implements ImportSelector {
  @Override
  public String[] selectImports(AnnotationMetadata importingClassMetadata) {
    return new String[]{"com.example.demo.bean.TestBean3"};
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import com.example.demo.bean.TestImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class,TestImportSelector.class})
@Configuration
public class AppConfig {
}

导入ImportBeanDefinitionRegistrar的实现类

package com.example.demo.bean;

public class TestBean4 {
}

package com.example.demo.bean;

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;

public class TestImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(TestBean4.class);
    registry.registerBeanDefinition("TestBean4", rootBeanDefinition);
  }
}

package com.example.demo;

import com.example.demo.bean.TestBean1;
import com.example.demo.bean.TestConfig;
import com.example.demo.bean.TestImportBeanDefinitionRegistrar;
import com.example.demo.bean.TestImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import({TestBean1.class,TestConfig.class,TestImportSelector.class,TestImportBeanDefinitionRegistrar.class})
@Configuration
public class AppConfig {
}

最后,我们来看下导入结果:

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

  @Test
  public void test() {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
    String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
    System.out.println("--------------------------------------------------------");
    for (String beanDefinitionName: beanDefinitionNames) {
      System.out.println(beanDefinitionName);
    }
    System.out.println("--------------------------------------------------------");
  }
}

打印结果如下:

--------------------------------------------------------
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
appConfig
com.example.demo.bean.TestBean1
com.example.demo.bean.TestConfig
getTestBean2
com.example.demo.bean.TestBean3
TestBean4
--------------------------------------------------------

可以看出TestBean1,TestBean2,TestBean3,TestBean4通过不同的4种导入方法被导入SpringIOC容器中。 

到此这篇关于浅谈Spring中@Import注解的作用和使用的文章就介绍到这了,更多相关Spring @Import注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • mybatis 集合嵌套查询和集合嵌套结果的区别说明

    mybatis 集合嵌套查询和集合嵌套结果的区别说明

    这篇文章主要介绍了mybatis 集合嵌套查询和集合嵌套结果的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • @Scheduled 如何读取动态配置文件

    @Scheduled 如何读取动态配置文件

    这篇文章主要介绍了@Scheduled 如何读取动态配置文件的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 数据同步利器DataX简介及如何使用

    数据同步利器DataX简介及如何使用

    DataX 是阿里云 DataWorks数据集成 的开源版本,使用Java 语言编写,在阿里巴巴集团内被广泛使用的离线数据同步工具/平台,今天给大家分享一个阿里开源的数据同步工具DataX,在Github拥有14.8k的star,非常受欢迎
    2024-02-02
  • 详解Maven私服Nexus的安装与使用

    详解Maven私服Nexus的安装与使用

    这篇文章主要介绍了详解Maven私服Nexus的安装与使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Java abstract class 与 interface对比

    Java abstract class 与 interface对比

    这篇文章主要介绍了 Java abstract class 与 interface对比的相关资料,需要的朋友可以参考下
    2016-12-12
  • Spring4整合Hibernate5详细步骤

    Spring4整合Hibernate5详细步骤

    本篇文章主要介绍了Spring4整合Hibernate5详细步骤,具有一定的参考价值,有兴趣的同学可以了解一下
    2017-04-04
  • SpringMVC @GetMapping注解路径冲突问题解决

    SpringMVC @GetMapping注解路径冲突问题解决

    MD5对密码进行加密存储是常见的一种加密方式,本文主要介绍了Java双重MD5加密实现安全登录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Mybatis-plus出现数据库id很大或者为负数的解决

    Mybatis-plus出现数据库id很大或者为负数的解决

    本文主要介绍了Mybatis-plus出现数据库id很大或者为负数的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • SpringMVC空指针异常NullPointerException解决及原理解析

    SpringMVC空指针异常NullPointerException解决及原理解析

    这篇文章主要介绍了SpringMVC空指针异常NullPointerException解决及原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • idea如何在service窗口中显示多个服务

    idea如何在service窗口中显示多个服务

    这篇文章主要介绍了idea如何在service窗口中显示多个服务问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09

最新评论