I currently live in jar/dependency hell. At one point there was about 5 jars for everything and statements like "We need to break things apart" swung the pendulum all the way to the other side. Left or the right, you pick :). Recently I was working on a project where we have old spring xml configuration meshed with new java config. There was some conflicting configuration classes so I needed to use a filter to customize scanning.
After digging into spring reference documentation there is a filter system that allows you to exclude elements during the component scan process. The following filter types are available:
|Filter Type||Example Expression||Description|
|annotation||org.example.SomeAnnotation||An annotation to be present at the type level in target components.|
|assignable||org.example.SomeClass||A class (or interface) that the target components are assignable to (extend/implement).|
|aspectj||org.example..*Service+||An AspectJ type expression to be matched by the target components.|
|regex||org\.example\.Default.*||A regex expression to be matched by the target components class names.|
|custom||org.example.MyTypeFilter||A custom implementation of the org.springframework.core.type .TypeFilter interface.|
Custom component scan using java config
The documentation shows the xml version but since we are on the path to eliminate XML, I wanted this in my ApplicationConfig java configuration file. When you go through this process of reverse engineering your xml to java config. The java config route uses the ComponentScan.Filter annotation and it contains 3 optional elements.
|value||The pattern which is an alternative to the Class element that allows you to specifiy a pattern.|
|type||Defined by the [FilterType Enum](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/FilterType.html) it specifies the type to use.|
|value||The classes or classes that should be used with the filter|
The java config and the associated xml configuration is below:
Spring is very configurable and while this shows off how filter components during auto scanning, there is also way to include. See spring docs for more details.