1.34context:component-scan的resource-pattern属性
戴着假发的程序员出品
[查看视频教程]
resource-pattern是用来配置要扫描的资源的正则表达式的,一般这里都是一个粗略的配置。
默认的配置是”**.*class“ 表示扫描配置包下的所有class文件。
我们可以修改测试以下。
准备两个类:
1 /** 2 * @author 戴着假发的程序员 3 * 4 * @description 5 */ 6 @Component 7 public class Person { 8 public Person(){ 9 System.out.println("实例化:Person"); 10 } 11 }
1 /** 2 * @author 戴着假发的程序员 3 * 4 * @description 5 */ 6 @Component 7 public class Student{ 8 public Student(){ 9 System.out.println("实例化Student"); 10 } 11 }
修改配置,只扫描dent结尾的类:
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 resource-pattern="**/*dent.class" base-package="com.boxuewa.dk.demo5"> 11 </context:component-scan> 12 </beans>
测试:
1 @Test 2 public void testResourcePattern(){ 3 ApplicationContext ac = 4 new ClassPathXmlApplicationContext("applicationContext.xml"); 5 }
结果:
我们会发现Person类不会被加载和实例化.
我是戴着假发的程序员,分享技术,分享经验,如果要转载,请注明:出自戴着假发的程序员