Reputation: 21425
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.
Upvotes: 2
Views: 4256
Reputation: 11363
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.
@Component
, and you'd use an annotation filter for that. Dao
, and you'd use assignable for that.com.foo.**.service.*
, and you'd use a regex for that.com.foo..service.* && !com.foo..MockService
, and you'd use aspectj for that. 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.
Upvotes: 6
Reputation: 1324
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.
Upvotes: 1