简体   繁体   English

Spring中的组件扫描过滤器类型

[英]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. 我正在使用Spring In Action第3版学习Spring,我在Spring的component scanning中遇到了不同的filter types

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. 在expression属性中指定要扫描的注释。

assignable - Filters scan classes looking for those that are assignable to the type specified in the expression attribute. assignable - 过滤扫描类,查找可分配给expression属性中指定类型的类。

aspectj - Filters scan classes looking for those that match the AspectJ type expression specified in the expression attribute. aspectj - 过滤扫描类,查找与expression属性中指定的AspectJ类型表达式匹配的扫描类。

custom - Uses a custom implementation of org.springframework.core.type.TypeFilter, as specified in the expression attribute. custom - 使用org.springframework.core.type.TypeFilter的自定义实现,如expression属性中指定的那样。

regex - Filters scan classes looking for those whose class names match the regular expression specified in the expression attribute. regex - 过滤扫描类,查找其类名与expression属性中指定的正则表达式匹配的类。

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. 组件扫描告诉Spring以递归方式查找包中的类,为找到的每个类实例化一个对象,并管理这些对象的生命周期。 The objects are called beans. 这些对象称为bean。 (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.) (这是一个非常粗略的解释; Spring检查范围,创建代理,并执行大量其他工作,但这些细节与讨论过滤器无关。)

A component scan filter narrows down which of those classes to instantiate beans for. 组件扫描过滤器缩小了实例化bean的类的范围。

  • You might only want to consider classes which have a certain annotation, eg @Component , and you'd use an annotation filter for that. 您可能只想考虑具有特定注释的@Component ,例如@Component ,并且您将使用注释过滤器。
  • You might want to consider classes that implement a certain interface, eg Dao , and you'd use assignable for that. 您可能想要考虑实现某个接口的类,例如Dao ,并且您可以使用assignable
  • You might want to pick out some classes by name, eg com.foo.**.service.* , and you'd use a regex for that. 您可能希望按名称选择一些类,例如com.foo.**.service.* ,并且您将使用正则表达式
  • 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. 您可能想要使用表达式来选择类的复杂子集,例如com.foo..service.* && !com.foo..MockService ,并且您将为此使用aspectj
  • 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. 您可能极少希望通过类的元数据来选择类,例如,如果该类具有一个名为Foo的封闭类,则创建一个bean,然后编写一个自定义 TypeFilter即可,从而可以访问该元数据。

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. 我从个人经验中按顺序列出了这些,我猜这个annotationassignable是迄今为止最受欢迎的。

Update: All filters are implemented as TypeFilter s, and they look at different pieces of class metadata in their match method. 更新:所有筛选器都实现为TypeFilter ,并且它们在其match方法中查看类元数据的不同部分。 For example, RegexPatternTypeFilter implements the regex filter, and its match method looks like 例如, RegexPatternTypeFilter实现了正则表达式过滤器,其match方法如下所示

@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. 编写自己的自定义 TypeFilter可以让您使用org.springframework.core.type.ClassMetadataorg.springframework.core.type.AnnotationMetadata接口中的方法来决定Spring是否应为具有某些特定元数据的类创建bean。

AspectJ type expression refers to pointcut expression used by AspectJ framework. AspectJ类型表达式是指AspectJ框架使用的切入点表达式。 AspectJ is framework for aspect oriented programming. AspectJ是面向方面的编程的框架。 More info here http://www.eclipse.org/aspectj/doc/next/progguide/semantics-pointcuts.html 此处的更多信息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 “自定义”意味着您可以提供自己的类来查找弹簧组件,而不必使用spring默认值

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. 基本上,此过滤器类型的工作方式与Aspectj过滤器类似,但不是使用Aspectj类型的表达式来查找组件,而是使用常规的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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