1、(先写一个实体类)User.class
package com.zdc.sp.model;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
2、(配置文件类)MyConfig.class
package com.zdc.sp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import com.zdc.sp.controller.UserController;
import com.zdc.sp.model.User;
import com.zdc.sp.service.UserService;
@Configuration //表示一个配置文件
//扫描制定区域包里面的注解
@ComponentScan(value = "com.zdc.sp",
excludeFilters = {
//@Filter(type=FilterType.ANNOTATION,value={Controller.class})
//@Filter(type=FilterType.ASSIGNABLE_TYPE,value={UserService.class})
@Filter(type=FilterType.CUSTOM,classes={MyFilter.class})
}
//includeFilters = {@Filter(type=FilterType.ANNOTATION,value={Controller.class})},useDefaultFilters=false
)
//@ComponentScans(
// value = {
// @ComponentScan(value = "com.zdc.sp",
// //excludeFilters = {@Filter(type=FilterType.ANNOTATION,value={Controller.class})}
// includeFilters = {@Filter(type=FilterType.ANNOTATION,value={Controller.class})},useDefaultFilters=false
// )
// }
// )
//@Filter 注解过滤
//excludeFilters 指定不扫描哪些包
//includeFilters 指定扫描哪些包;注意:useDefaultFilters=false
//type=FilterType.ANNOTATION 根据注解过滤注解
//type=FilterType.ASSIGNABLE_TYPE 根据类型过滤注解
//type=FilterType.CUSTOM 根据自定义规则过滤注解
public class MyConfig {
//添加一个bean
@Bean
public User user(){
return new User("张三",11);
}
}
3、自定义规则类
package com.zdc.sp.config;
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 MyFilter implements TypeFilter{
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
//获取注解类信息
AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
//获取当前正在扫描类的信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前类资源(类的路径)
Resource resource = metadataReader.getResource();
String className = classMetadata.getClassName();
//自定义规则扫描注解
if (className.contains("ao")) {
return true;//返回true则不扫描
}
System.out.println("####################:"+className);
return false;
}
}
4、测试
package com.zdc.sp;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.zdc.sp.config.MyConfig;
import com.zdc.sp.model.User;
public class Main {
public static void main(String[] args) {
//获取配置文件
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
//获取bean
//根据类型获取bean
//User user = context.getBean(User.class);
//根据名称name 获取bean
User user = (User) context.getBean("user");
String[] str = context.getBeanDefinitionNames();
for (String string : str) {
System.out.println(string);
}
System.out.println(user);
}
}