First look at the configuration file:
<?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.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsd "><context:component-scan base- Package= "Cn.outofmemory.spring" use-default-filters= "false" > <context:include-filter type= "regex" expression= "cn\.outofmemory\.spring\. [^.] + (dao| Service) "/> </context:component-scan></beans>
In this file, there is only one Context:component-scan node under the Beans root node, and this node has two properties Base-package property tells spring to scan the package, use-default-filters= "false" Indicates that you do not use the default filter, where the default filter scans for classes that contain service,component,repository,controller annotation adornments. If you do not want to use the default filter, you can set the Use-default-filters property to False.
The Context:component-scan node allows two sub-nodes <context:include-filter> and <context:exclude-filter>. The type and expression of the filter tag are described below:
Filter Type |
Examples Expression |
Description |
Annotation |
Org.example.SomeAnnotation |
Target class conforming to Someannoation |
Assignable |
Org.example.SomeClass |
Specify the full name of class or interface |
Aspectj |
Org.example. *service+ |
AspectJ Language method |
Regex |
Org\.example\. default.* |
Regelar Expression |
Custom |
Org.example.MyTypeFilter |
Spring3 new custom type, actually org.springframework.core.type.TypeFilter |
In our example, the type of filter is set to a regular expression, regex, in the regular. Represents all characters, while \. is the true. Character . Our regular represents a class that ends with a DAO or service.
We can also use annotaion to qualify, as follows:
<context:component-scan base-package = "cn.outofmemory.spring" use-default-filters= "false "> <context:include-filter type="annotation"expression=" Org.springframework.stereotype. Repository"/> <context:include-filter type="annotation"expression=" Org.springframework.stereotype. Service"/> </context:component-scan>
The type of Include-filter we specify here is Annotation,expression, which is the full name of the annotation class. Therefore, only classes that @Repository and @service annotations are scanned.
Another Context:conponent-scan node <context:exclude-filter> can be used to specify which classes to exclude, with the same usage as include-filter.
<context:component-scan>