Lecture Spring annotation-driven - @ Filter Introduction

Last lecture notes aimed at @ComponentScan done some explanation, this paper @Filter scanning conditions, and do some detailed introduction

1, FilterType.ANNOTATION accordance scanned back annotation attribute classes, the type of annotation, such as:

@ Configuration // marked as such configuration class 
@ComponentScan (value = "com.wxj", excludeFilters = { 
        @Filter (type = FilterType.ANNOTATION, classes Controller.class = {}) 
}) 
public class MainConfig { 
    // the @Controller marked excluded class annotated not loaded into the container to 
}
Copy the code

2, type = FilterType.ASSIGNABLE_TYPE, according to the specified class, filtered, the value of the attribute classes behind the "class name .class" as:

Copy the code
@ Configuration // marked as such configuration class 
@ComponentScan (value = "com.wxj", 
includeFilters = { 
        @Filter (type = FilterType.ASSIGNABLE_TYPE, classes BookService.class = {}) 
}, = useDefaultFilters to false) 
public class MainConfig { 
    // only loads BookService, and BookService or a subclass implementation class 
}

The above two methods commonly used in filtration.

3, FilterType.CUSTOM, own custom manner to filter and filter (this filter type used, although more complicated, but can use the rules defined by the scan to themselves)

@Filter first define the type of annotation

@ Configuration // marked as such configuration class 
@ComponentScan (value = "com.wxj", 
includeFilters @Filter = {(type = FilterType.CUSTOM, MyTypeFilter.class classes {}) 
}, = useDefaultFilters to false) 
public class MainConfig { 
}
When matching is the method of their return match method defined in MyTypeFilter MyTypeFilter class, which is true, to meet the filter criteria, if it returns false, do not meet the filter criteria, as follows:
The TypeFilter {class MyTypeFilter the implements public 

    / ** 
     * MetadataReader metadataReader: acquiring information currently being scanned class 
     * MetadataReaderFactory metadataReaderFactory, obtain additional information with any class 
     * 
     * / 
    public Boolean match (MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { 
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata (); // get information about the current annotation class 
        ClassMetadata classMetadata = metadataReader.getClassMetadata (); // get the current scan type information 
        resource resource = metadataReader.getResource (); // get the resource information about the current scanned 
        String name = classMetadata.getClassName (); // Get the name of the class 
        } 
        if (name.contains ( "er") ) {
            return true; // the name if the class with the "er", matching the filter requirements 
        return to false; 
    } 

}

Note: TypeFilter in ASPECTJ and REGEX (regular way), there is no introduction, use less, are interested can study on their own.

Guess you like

Origin www.cnblogs.com/toSeeMyDream/p/12071433.html