//配置类==配置文件(xml)
@Configuration //告诉Spring这个一个配制类
@ComponentScan(value="com.ceshi",includeFilters={
@Filter(type=FilterType.CUSTOM,classes={
MyFilterType.class
})
})
//value:指定要扫描的包。
//excludeFilters=Filter[] 扫描排除
//includeFilters=Filter[] 只扫描
//FilterType.ANNOTION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型
//FilterType.REGEX:正则表达式
//FilterType.CUSTOM:自定义
@ComponentScans(value={})//扫描多个包
public class BeanConfig {
//给容器注册一个Bean,类型class:默认为返回值的类型,标识id:默认为方法名
@Bean("person")
public PersonBean personBean(){
return new PersonBean("lishi","20");
}
}
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
public class MyFilterType implements TypeFilter {
/**
* MetadataReader:读取当前正在扫描的类信息
* MetadataReaderFactory:MetadataReader的工厂,根据工厂可以得到其他的MetadataReader类信息
*/
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
// TODO Auto-generated method stub
//获取当前类的注解信息
AnnotationMetadata AnnotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前正在扫描的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前类资源(类的路径)
Resource resource = metadataReader.getResource();
System.out.println(classMetadata.getClassName());
return true;
}
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.ceshi.config.BeanConfig;
public class IOCTest {
@Test
public void test01(){
ApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
String[] names = annotationConfigApplicationContext.getBeanDefinitionNames();
for(String name:names){
System.out.println(name);
}
}
}
com.ceshi.bean.PersonBean
com.ceshi.demo.MyFilterType
com.ceshi.IOCTest
com.ceshi.MainTest
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
beanConfig
personBean
personController
personDao
myFilterType
IOCTest
mainTest
personService
person