<context:component-scan>

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.