@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AliasFor {
@AliasFor("attribute")
String value() default "";
@AliasFor("value")
String attribute() default "";
Class<? extends Annotation> annotation() default Annotation.class;
}
1、ANNOTATION: 默认值,三层注解类型@Controller、@Service、@Repository -> @Component
2、excludeFilters:排除类,类型类枚举类型, 通过注解可查看类型
//扫描 org.zq 包下的@Component等注解 ,并排除 Service.class, Repository.class
@ComponentScan(value = "org.zq",
excludeFilters ={
@ComponentScan.Filter(type = FilterType.ANNOTATION,
value = {Service.class, Repository.class})
})
3、ASSIGNABLE_TYPE:值得是具体的一个类 如 StudentController.class
4、includeFilters:有默认行为,可以通过useDefaultFilters = false 禁止
//扫描 org.zq 包下的 StudentController.class 类纳入IOC容器
@ComponentScan(value = "org.zq", useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {StudentController.class}))
5、CUSTOM :自己定义包含规则
public class MyFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
ClassMetadata classMetadata = metadataReader.getClassMetadata();//获取元信息
// 扫描器扫描“org.zq”包中所有的类 ,getClassName() 可以拿到该包中所有类的名字(只有三层)
String className = classMetadata.getClassName();
//如果className包含School 则加入ioc容器
if(className.contains("School")){
return true;
}
return false;
}
}
配置类中 使用
@ComponentScan(value = "org.zq", useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(
type = FilterType.CUSTOM, value = {MyFilter.class}
))
@Bean("stu")
public Student myStudent() {
return new Student("ls", 23);
}
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{ Student.class.getName() }; //必须给全类名
}
}
在配置类中导入
@Import(MyImportSelector.class)
3、 import方式三 : 编写一个类 实现 ImportBeanDefinitionRegistrar 接口 ,并 通过方式一讲导入类引入
public class MyImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition root = new RootBeanDefinition(Student.class);
registry.registerBeanDefinition("student",root);
}
}
4、FactoryBean方式
1、实现FactoryBean 接口
public class MyFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
return new Student();
}
@Override
public Class<?> getObjectType() {
return Student.class;
}
@Override
public boolean isSingleton() {
return false;//是否是单例
}
}
@Bean
public FactoryBean<Student> student(){
return new MyFactoryBean(); //到底放的是哪一个对象 & 区分
}
Test
Object apple = context.getBean("&student"); //获取的是MyFactoryBean的实例
Object apple = context.getBean("student"); //获取的是Student的实例
a. 非三层组件 :Student\Teacher\配置信息 : @Bean
b. 三层组件 :Controller、Service、Dao :@Component(@Controller、@Service、@Repository)
a.加入@Component等注解
b.配置,让容器识别注解
<context:component-scan base-package=" 包名 " ></context:component-scan>
@Configuration
@ComponentScan(value="包名")
public class MyConfig {...}
FilterType :
@Component(@Controller、@Service、@Repository):三层组件
非三层组件