Spring注解之@Import和@ImportResource

前言

为什么研究这两个注解,是因为在看Spring Boot源码的时候,对于其中大量的EnableXXX都使用了@Import注解,所以决定研究下这个注解,顺便写了一点关于@ImportRescource注解的东西,希望对大家有帮助。

@Import

简介:功能类似XML配置的,用来导入配置类,可以导入带有@Configuration注解的配置类或实现了ImportSelector/ImportBeanDefinitionRegistrar,或者导入普通的POJO(Spring会将其注册成Spring Bean,导入POJO需要使用Spring 4.2以上)。

下面注解源码,源码有比较详细的英文解释:

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

Provides functionality equivalent to the {@code } element in Spring 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}). * *

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

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

If XML or other non-{@code @Configuration} bean definition resources need to be * imported, use the {@link ImportResource @ImportResource} annotation instead. * * @author Chris Beams * @author Juergen Hoeller * @since 3.0 * @see Configuration * @see ImportSelector * @see ImportResource */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import { /** * {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar} * or regular component classes to import. */ Class[] value(); }

关于Configuration和POJO,没什么好说的,就是简单的配置。这里说下ImportSelector和ImportBeanDefinitionRegistrar的作用(虽然开发过程中很少会使用到,但是Spring和Spring Boot的源码大面积使用了这个功能)。

ImportSelector

  • 根据AnnotationMetadata(实质上是引入@Import注解的类的注解信息),来判断是否引入哪些配置类,通过字符串数组的方式返回配置类的全限定名。

DeferredImportSelector

  • ImportSelector的子接口,区别是,他会在所有的@Configuration类加载完成之后再加载返回的配置类。ImportSelector会在当前Configuration类加载之前去加载返回的配置类。
  • 可以使用@Order注解或者Ordered接口来指定DeferredImportSelector的加载顺序。
  • 并且提供了新的方法getImportGroup()用来跨DeferredImportSelector实现自定义Configuration的加载顺序。

ImportBeanDefinitionRegistrar

  • 根据AnnotationMetadata(实质上是引入@Import注解的类的注解信息), 来注册BeanDenfinition,通过BeanDefinitionRegistry实例的register可以注册BeanDefinition。

三个接口的使用例子在文章最后面。

@ImportResource

和@Import类似,区别就是@ImportResource导入的是配置文件。

同样源码如下

/**
 * Indicates one or more resources containing bean definitions to import.
 *
 * 

Like {@link Import @Import}, this annotation provides functionality similar to * the {@code } element in Spring XML. It is typically used when designing * {@link Configuration @Configuration} classes to be bootstrapped by an * {@link AnnotationConfigApplicationContext}, but where some XML functionality such * as namespaces is still necessary. * *

By default, arguments to the {@link #value} attribute will be processed using a * {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader} * if ending in {@code ".groovy"}; otherwise, an * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader} * will be used to parse Spring {@code } XML files. Optionally, the {@link #reader} * attribute may be declared, allowing the user to choose a custom {@link BeanDefinitionReader} * implementation. * * @author Chris Beams * @author Juergen Hoeller * @author Sam Brannen *

你可能感兴趣的:(Spring,JAVA,Spring)