SpringBoot的@Import和@ImportResource


  1. @Import
    • 源碼  
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

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

}
    • 使用說明  
    1. 導入@Configuration注解的配置類(4.2版本之前只可以導入配置類,4.2版本之后也可以導入普通類)
    2. 導入ImportSelector的實現類
    3. 導入ImportBeanDefinitionRegistrar的實現類

 

  1. @ImportResource
    • 源碼
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE)
      @Documented
      public @interface ImportResource {
      
          /**
           * Alias for {@link #locations}.
           * @see #locations
           * @see #reader
           */
          @AliasFor("locations")
          String[] value() default {};
      
          /**
           * Resource locations from which to import.
           * <p>Supports resource-loading prefixes such as {@code classpath:},
           * {@code file:}, etc.
           * <p>Consult the Javadoc for {@link #reader} for details on how resources
           * will be processed.
           * @since 4.2
           * @see #value
           * @see #reader
           */
          @AliasFor("value")
          String[] locations() default {};
      
          /**
           * {@link BeanDefinitionReader} implementation to use when processing
           * resources specified via the {@link #value} attribute.
           * <p>By default, the reader will be adapted to the resource path specified:
           * {@code ".groovy"} files will be processed with a
           * {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader GroovyBeanDefinitionReader};
           * whereas, all other resources will be processed with an
           * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader XmlBeanDefinitionReader}.
           * @see #value
           */
          Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
      
      }
    • 使用說明

       @ImportResource等同於xml配置:<import resource="配置文件.xml" />


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM