戴着假发的程序员出品
spring应用手册(第一部分)
我们已经知道context:component-scan的配置可以通知spring扫描拥有spring标准注解的类。这些标注大致是:@Component、@Controller、@Service、@Repository。但是我们也可以通过 context:include-filter配置通知spring扫描一些没有标准注解但是我们希望spring帮我们管理的类。context:include-filter有两个必须的属性:
type: 配置filter的类型,这个类型一共有以下五个值:
assignable-指定扫描某个接口派生出来的类 annotation-指定扫描使用某个注解的类 aspectj-指定扫描AspectJ表达式相匹配的类 custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类 regex-指定扫描符合正则表达式的类
expression: 根据type的不同,这个表达式的配置方式也不同。
我们直接看案例:
[1]assignable-指定扫描某个接口派生出来的类:
我们准备如下结构的类: 其中Info接口:
/** * @author 戴着假发的程序员 * * @description */ public interface Info { }Person类: 注意,我们在Person类上方添加了标准注解@Component,所以Person类会被spring加载
/** * @author 戴着假发的程序员 * * @description */ @Component public class Person { }Student类:Student类没有标注注解,理论上不会被Spring加载。但是Student类实现了接口Info
/** * @author 戴着假发的程序员 * * @description */ public class Student implements Info { }我们的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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.boxuewa.dk.demo5"> <!-- 通知spring将实现接口Info的类也加载进来 --> <context:include-filter type="assignable" expression="com.boxuewa.dk.demo5.Info"/> </context:component-scan> </beans>测试:
@Test public void testIncoudFilter(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-demo9.xml"); System.out.println(ac.getBean(Person.class)); System.out.println(ac.getBean(Student.class)); }结果: 我们发现spring已经帮我们加载了没有注解的Student类。
[2]annotation-指定扫描使用某个注解的类:
我们自己创建一个注解:
/** * @author 戴着假发的程序员 * * @description */ @java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE}) @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) public @interface DemoAno { }修改上面的Student类,Studnet类不再实现接口Info,但是在Student类上添加我们自己的注解。
/** * @author 戴着假发的程序员 * * @description */ @DemoAno public class Student { }修改配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.boxuewa.dk.demo5"> <!-- 通知spring将有指定注解的类加载进来 --> <context:include-filter type="annotation" expression="com.boxuewa.dk.demo5.annotation.DemoAno"/> </beans>在测试: 我们发现Student类依然可以被加载。
[3]aspectj-指定扫描AspectJ表达式相匹配的类:比如要求加载某个类的派生类
这里需要添加AspectJ的依赖:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.3.RELEASE</version> </dependency>我们添加一个新的User类:
/** * @author 戴着假发的程序员 * * @description */ public class User { }继续修改我们的Stdeunt类,不实现接口,不需要注解,只要继承User类即可。
/** * @author 戴着假发的程序员 * * @description */ public class Student extends User { }修改配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.boxuewa.dk.demo5"> <!-- 通知spring所有继承和扩展自指定类的类全部加载进来 --> <context:include-filter type="aspectj" expression="com.boxuewa.dk.demo5.User+"/> </context:component-scan> </beans>再测试: 我们会发现Stduent类依然会被spring加载。
[4]custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类
我们继续修改Student类,不实现接口,不添加注解,不继承任何类。
/** * @author 戴着假发的程序员 * * @description */ public class Student{ }我们添加一个MyFilter实现TypeFilter接口:
/** * @author 戴着假发的程序员 * * @description */ public class MyFilter implements TypeFilter { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //这里判断读取的类型是否是Student,如果是就返回true,否则返回false; //返回true就会被spring加载,否则不加载 if(metadataReader.getClassMetadata().getClassName().equals(Student.class.getName())){ return true; } return false; }修改配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.boxuewa.dk.demo5"> <!-- 通知spring根据我们配置的MyFilter类进行加载 这里的 expression要配置我们自己的MyFilter --> <context:include-filter type="custom" expression="com.boxuewa.dk.demo5.MyFilter"/> </context:component-scan> </beans>测试: 我们的Student类依然可以被加载。
[5]regex-指定扫描符合正则表达式的类
这个是比较简单的,就是通过我们给出的正则表达式进行匹配。
我们继续修改配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.boxuewa.dk.demo5"> <!-- 通过spring根据我们给出的正则表达式进行匹配 --> <context:include-filter type="regex" expression=".*.*dent"/> </context:component-scan> </beans>在测试: 依然可以成功的加载Student类。