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