Spring (16) - bean definition using annotations (2)

16.5 Filters

By default, Spring will scan all @Component, @Controller, @Service and @Repository annotated classes, as well as classes annotated by annotations derived from these annotations, such as the custom @MyComponent annotation introduced earlier, and Define them as a bean in the corresponding bean container. Spring provides two filter definitions that allow us to further filter the classes that need to be scanned, one is <context:include-filter/>and the other is <context:exclude-filter/>. The configuration of these two filters is basically the same, the difference is their function. <context:include-filter/>Filter the content that needs to be scanned, that is, tell Spring what content needs to be scanned in addition to scanning according to the default logic. Instead <context:exclude-filter/>, it is used to filter content that does not need to be scanned, that is, to tell Spring which content that should be scanned should be excluded from scanning. They are all <context:component-scan/>defined as sub-elements. We can <context:component-scan/>define multiple <context:include-filter/>or multiple at the same time <context:exclude-filter/>under one label, but if we define and at the <context:component-scan/>same time under one label, we need to define them first and then define them .<context:include-filter/><context:exclude-filter/><context:include-filter/><context:exclude-filter/>

When defining <context:include-filter/>or <context:exclude-filter/>we must specify the type attribute and expression attribute. The type attribute indicates which type to filter by, and the expression indicates the expression that needs to be filtered.

For the type attribute, we can choose the following five types of values.

  • Annotation: Represents an annotation, and scans the target class according to the annotation, that is, which annotation should be used for the target class to be annotated, and the corresponding expression should be the full name of the annotation.
  • Assignable: Indicates which class or interface is derived from, that is, which class the target class inherits or which interface it implements, and the corresponding expression should be the full name of the corresponding interface or class.
  • aspect: Indicates that an expression of type Aspect will be used to represent the target class.
  • regex: Indicates that a regular expression will be used to match the target class name.
  • custom: Indicates that a custom one will be used org.springframework.core.type.TypeFilterto match the corresponding class.

16.5.1 annotation

The following example means that we will exclude the scanning of classes annotated with the @Service annotation, that is, the corresponding Class will not be added to the bean container.

<context:component-scan base-package="com.app">	
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

To reiterate here, that is, when using Spring, by default, all packages specified by base-package and their descendants will be scanned using @Component, @Controller, @Service and @Repository and other annotations using these annotations. Annotations, classes marked with these annotations will be added to the bean container. We can <context:include-filter/>add other Classes that are not in these categories to the bean container, and <context:exclude-filter/>we can exclude the Classes in this category from being added to the category of the bean container.

So we can also <context:include-filter/>add the Class that is annotated with a certain type of annotation to the bean container. In the following example, we define the previously introduced @MyComponent as a common annotation, and then incorporate it into the category of the bean container to allow Spring to add the class annotated with @MyComponent to the bean container.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyComponent {

	public String value() default "";
	
}
<context:component-scan base-package="com.app">	
	<context:include-filter type="annotation" expression="com.app.MyComponent"/>
</context:component-scan>

16.5.2 assignable

assignable means to scan based on inherited classes or implemented interfaces. The following means that all classes that inherit or implement java.lang.Object will be scanned and added to the bean container. In this way, we can let Spring add all classes under certain packages to Spring's bean container.

<context:component-scan base-package="com.app">	
	<context:include-filter type="assignable" expression="java.lang.Object"/>
</context:component-scan>

16.5.3 aspect

When the specified type is aspect, it means that the corresponding class that needs to be matched will be specified in the form of Spring-defined aspect. The following example means to match all the classes whose names start with Bean in the com.app package and its descendants, and add them to the bean container. When using this type, you need to add spring-aspects-xxx.jar to the classpath.

<context:component-scan base-package="com.app">	
	<context:include-filter type="aspectj" expression="com.app..Bean*"/>
</context:component-scan>

16.5.4 regex

regex means that a regular expression will be used for matching. The following example indicates that all classes whose names start with Bean are matched by regular expressions, and they are added to the bean container (dot "." represents any character in the regular expression).

<context:component-scan base-package="com.app">	
	<context:include-filter type="regex" expression=".*\.Bean.*"/>
</context:component-scan>

16.5.5 custom

custom means use your own defined TypeFilter for matching. Let's look at an example. Let's define our own TypeFilter, which simply matches the name of the class. As long as the name of the class contains Bean, the matching is successful.

public  class  MyTypeFilter  implements  TypeFilter {

	public boolean match(MetadataReader metadataReader,
			MetadataReaderFactory metadataReaderFactory) throws IOException {
		String className = metadataReader.getClassMetadata().getClassName();
		return className.contains("Bean");
	}

}

After that, we can specify the type as custom when defining the filter, and then specify our custom TypeFilter for the corresponding expression attribute.

<context:component-scan base-package="com.app">	
	<context:include-filter type="custom" expression="com.app.MyTypeFilter"/>
</context:component-scan>

(Note: This article is written based on Spring 4.1.0)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326341504&siteId=291194637
Recommended