<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
//context命名空间
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-3.0.xsd">
<context:component-scan base-package="com.sparta.trans" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
base-package:
告诉spring要扫描的包;
use-default-filters=”false”:
表示不要使用默认的过滤器。
use-default-filters=”true”或不设置时表示会扫描包含Service,Component,Responsitory,Controller注释修饰类。
<context:include-filter/>:
<context:include-filter>为<context:annotation-config/>的子标签(另一个相对应的子标签为:<context:exclude-filter>);
而<context:component-scan>包含了<context:annotation-config/>;
filter标签的type和表达式说明如下:
Filter Type | Examples Expression | Description |
annotation |
org.example.SomeAnnotation |
符合SomeAnnoation的target class |
assignable |
org.example.SomeClass |
指定class或interface的全名 |
aspectj |
org.example..*Service+ |
AspetJ语法 |
regex |
org.example.Default.* |
Regelar Expression |
custom |
org.example.MyTypeFilter |
Spring3新增自订Type,称作org.springframework.core.type.TypeFilter |