Java:Spring中context扫描器和过滤器的使用
扫描器:
use-default-filters属性是使用默认过滤器,默认值为true,可同时扫描多个位置
<?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.atguigu,[逗号隔开可以加如多个扫描路径]"></context:component-scan>
</beans>
一个配置文件可以配置多个context:component-scan标签;
扫描器中加入过滤器
过滤器:
exclude-filter:装载时排除指定包
include-filter: 装载时指定包
在扫描器中使用过滤器:
<!-- 示例一扫描含Controller的类-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 实例二扫描不含Controller的类-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>