@Import
开发使用的场景不多,主要功能是导入一个Bean,让spring 容器装载
代码说明
public @interface Import {
/**
导入一个类名称
* {@link Configuration @Configuration}, {@link ImportSelector},
* {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
*/
Class<?>[] value();
}
1:导入普通的类
public class TestA {
public void fun(String str) {
System.out.println(str);
}
public void printName() {
System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
}
}
@Import({TestA.class})
@Configuration
public class ImportConfig {
}
2:在ImportConfig.class里面直接引入TestB
@Configuration
public class TestB {
public void fun(String str) {
System.out.println(str);
}
public void printName() {
System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
}
}
@Import({TestA.class,TestB.class})
@Configuration
public class ImportConfig {
}
3:通过ImportSelector 方式导入的类
public class TestC {
public void fun(String str) {
System.out.println(str);
}
public void printName() {
System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
}
}
public class SelfImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.test.importdemo.TestC"};
}
}
@Import({TestA.class,TestB.class,SelfImportSelector.class})
@Configuration
public class ImportConfig {
}
4: 通过 ImportBeanDefinitionRegistrar 方式导入的类
public class TestD {
public void fun(String str) {
System.out.println(str);
}
public void printName() {
System.out.println("类名 :" + Thread.currentThread().getStackTrace()[1].getClassName());
}
}
public class SelfImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition root = new RootBeanDefinition(TestD.class);
registry.registerBeanDefinition("testD", root);
}
}
@Import({TestA.class,TestB.class,SelfImportSelector.class,
SelfImportBeanDefinitionRegistrar.class})
@Configuration
public class ImportConfig {
}
源码讲解
源码解析标记org.springframework.context.annotation.ConfigurationClassParser#processImports
参考链接 :https://blog.csdn.net/mamamalululu00000000/article/details/86711079
@ComponentScan
@ComponentScan 的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
@AliasFor("basePackages")
String[] value() default {};
@AliasFor("value")
String[] basePackages() default {};
Class<?>[] basePackageClasses() default {};
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
String resourcePattern() default "**/*.class";
boolean useDefaultFilters() default true;
ComponentScan.Filter[] includeFilters() default {};
ComponentScan.Filter[] excludeFilters() default {};
boolean lazyInit() default false;
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Filter {
FilterType type() default FilterType.ANNOTATION;
@AliasFor("classes")
Class<?>[] value() default {};
@AliasFor("value")
Class<?>[] classes() default {};
String[] pattern() default {};
}
}
参考链接:https://blog.csdn.net/u012326462/article/details/82765485
@ImportResource
@ImportResource注解:用于导入 Spring 的 xml 配置文件,让该配置文件中定义的 bean 对象加载到Spring容器中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--将 HelloService 以xml的方式,注入到容器中-->
<bean id="helloService" class="com.demo.springboot.service.HelloService"></bean>
</beans>
/**
* Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
* 如果想让Spring的配置文件生效,加载到Spring 容器中来;
* 使用@ImportResource注解,将其标注在一个配置类上(此处配置在启动类)
*/
@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class BootApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(BootApplication.class,args);
}
}
@Import是用来导入java类/配置类的,而@ImportResorce是用来导入配置文件的