spring应用手册-IOC(XML配置实现)-(34)-context:component-scan的resource-pattern属性

    科技2022-08-04  120

    戴着假发的程序员出品

    context:component-scan的resource-pattern属性

    spring应用手册(第一部分)

    resource-pattern是用来配置要扫描的资源的正则表达式的,一般这里都是一个粗略的配置。

    默认的配置是”**.*class“ 表示扫描配置包下的所有class文件。

    我们可以修改测试以下。

    准备两个类:

    /** * @author 戴着假发的程序员 * * @description */ @Component public class Person { public Person(){ System.out.println("实例化:Person"); } } /** * @author 戴着假发的程序员 * * @description */ @Component public class Student{ public Student(){ System.out.println("实例化Student"); } }

    修改配置,只扫描dent结尾的类:

    <?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 resource-pattern="**/*dent.class" base-package="com.boxuewa.dk.demo5"> </context:component-scan> </beans>

    测试:

    @Test public void testResourcePattern(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); }

    结果: 我们会发现Person类不会被加载和实例化.

    Processed: 0.014, SQL: 8