转载

spring源码分析之@Import注解

Indicates one or more {@link Configuration @Configuration} classes to import.

表示一个或多个类会被导入,参考@Configuration.

Provides functionality equivalent to the {@code } element in Spring XML.

提供在xml中 标签相同的功能.

Allows for importing {@code @Configuration} classes, {@link ImportSelector} and {@link ImportBeanDefinitionRegistrar} implementations,as well as regular component classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).

允许加上导入类,ImportSelector和ImportBeanDefinitionRegistrar的实现,等同于正常的组件类(4.2开始;类似于AnnotationConfigApplicationContext.register).

AnnotationConfigRegistry接口是从4.1开始的

{@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.

在导入类里定义的Bean应该可以被Autowired注入访问。bean自身或者定义bean的配置类都能被autowired。后者能在类方法之间被显式的导航.

May be declared at the class level or as a meta-annotation.

在class层级定义或者当做一个meta-annotation。

If XML or other non-{@code @Configuration} bean definition resources need to be imported, use the {@link ImportResource @ImportResource} annotation instead.

如果xml或者其他没有@Configuration的bean资源需要被导入,那就使用@ImportResource注解来替代。

参考资料:

  1. Spring Import 三种用法与源码解读
  2. Spring @Import注解 —— 导入资源
原文  http://johnwonder.coding.me/2019/07/06/spring-import-annotation/
正文到此结束
Loading...