SpringBoot - @Import注解
@Import
作用:用来导入配置类或者一些需要前置加载的类,注入到IOC容器当中
范围:类上,一般使用在被@Configuration修饰的类上
参数:value 数据类型为class[]
注入普通类
@Configuration @Import({Object.class,Student.class}) public class MyConfiguration { }
注入带有@Configuration的配置类,一般在自定义start中会用到
@Configuration @Import({ConfigurationImport1.class,ConfigurationImport2.class}) public class MyConfiguration { } @Configuration public class ConfigurationImport1{ } @Configuration public class ConfigurationImport2{ }
通过ImportSelector 方式导入类
@Configuration @Import({MyImportSelector.class}) public class MyConfiguration { } class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { String[] classArray=new String[]{ "com.example.bean.Student", "com.example.bean.Teacher" }; return classArray; } }
posted on 2022-12-20 16:58 Mikasa-Ackerman 阅读(65) 评论(0) 编辑 收藏 举报