Spring注解之@Import

前置

如何动态导入Bean?
@Import注解的作用

  • 通常用于导入@Configuration类型
  • 导入普通的Bean
  • 实现ImportSelector类型
  • 实现ImportBeanDefinitionRegistrar
/**
 * Indicates one or more <em>component classes</em> to import &mdash; typically
 * {@link Configuration @Configuration} classes.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} 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}).
 *
 * <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 the {@link ImportResource @ImportResource} annotation instead.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportBeanDefinitionRegistrar
 * @see ImportResource
 */
@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();

}

Demo

package com.dite;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Arrays;

/**
 * Create by dgx 2022/4/1 23:21
 */
@Import({Person.class,FruitConfiguration.class,SelectImportInstance.class,MyImportBeanDefinitionRegistrar.class})
public class ImportDemo {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportDemo.class);


        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {

            System.out.println(beanDefinitionName);
        }
    }

}

/**
 * 普通类
 */
class Person{

}
@org.springframework.context.annotation.Configuration
class  FruitConfiguration{

   @Bean
    Fruit fruit(){
       return  new Fruit();
   }
}

class Fruit{

}

/**
 * 通过实现ImportSelector 导入
 */
class  SelectImportInstance implements ImportSelector{

    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{Bike.class.getName()};
    }
}
class  Bike{

}

/**
 *  通过实现ImportSelector 导入
 */
class  MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        registry.registerBeanDefinition("plane",new RootBeanDefinition(Plane.class));

    }
}
class  Plane{

}
class  BooK{

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容