简体   繁体   中英

Component scanning filter types in Spring

I am learning Spring using Spring In Action 3rd Edition , I came across the different filter types in component scanning of Spring.

Here is the list available:

annotation - Filters scan classes looking for those annotated with a given annotation at the type level. The annotation to scan for is specified in the expression attribute.

assignable - Filters scan classes looking for those that are assignable to the type specified in the expression attribute.

aspectj - Filters scan classes looking for those that match the AspectJ type expression specified in the expression attribute.

custom - Uses a custom implementation of org.springframework.core.type.TypeFilter, as specified in the expression attribute.

regex - Filters scan classes looking for those whose class names match the regular expression specified in the expression attribute.

I got some idea on the use of the filter types for assignable and annotation based on examples given in book.

But for the remaining filter types, I am not able to understand how these types are used and when we need to use one of them. Can you please help me in understanding the concepts here.

A component scan tells Spring to recursively look for classes in a package, instantiate an object for each class that's found, and manage the lifecycle of those objects. The objects are called beans. (That's a very coarse explanation; Spring checks scopes, creates proxies, and does a ton of other stuff, but those details aren't relevant for talking about filters.)

A component scan filter narrows down which of those classes to instantiate beans for.

  • You might only want to consider classes which have a certain annotation, eg @Component , and you'd use an annotation filter for that.
  • You might want to consider classes that implement a certain interface, eg Dao , and you'd use assignable for that.
  • You might want to pick out some classes by name, eg com.foo.**.service.* , and you'd use a regex for that.
  • You might want to use expressions to pick out a complex subset of classes, eg com.foo..service.* && !com.foo..MockService , and you'd use aspectj for that.
  • You might extremely rarely want to pick out classes by their metadata, eg create a bean if the class has an enclosing class named Foo , and you'd write a custom TypeFilter to do that, which gives you access to that metadata.

I've listed these in order of popularity from my personal experience, and I'd guess that annotation , assignable are by far the most popular.

Update: All filters are implemented as TypeFilter s, and they look at different pieces of class metadata in their match method. For example, RegexPatternTypeFilter implements the regex filter, and its match method looks like

@Override
protected boolean match(ClassMetadata metadata) {
    return this.pattern.matcher(metadata.getClassName()).matches();
}

Writing your own custom TypeFilter lets you use the methods in the org.springframework.core.type.ClassMetadata and org.springframework.core.type.AnnotationMetadata interfaces to decide whether Spring should create a bean for a class with some particular metadata.

AspectJ type expression refers to pointcut expression used by AspectJ framework. AspectJ is framework for aspect oriented programming. More info here http://www.eclipse.org/aspectj/doc/next/progguide/semantics-pointcuts.html

"Custom" means that you can provide your own class for finding spring components instead of using spring defaults

Regex means regular expression. Basicly this filter type works similar to aspectj filter but instead of finding components using aspectj type expression it uses normal regular expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM