Spring 不能 include/exclude bean 在 @ComponentScan 中使用带星号的 REGEX 类型过滤器
Spring cannot include/exclude beans using filters of type REGEX with an asterisk in @ComponentScan
我正在尝试使用 @ComponentScan
注释的 IncludeFilters 属性 来管理我的 spring 应用程序加载的 bean。我使用 FilterType.REGEX
类型的过滤器。我想匹配任何东西作为我模式的最后一部分使用但我似乎根本不那样工作。
我有一个 bean 定义:
package org.example.child;
public class ChildDao {}
...
public class ChildService{}
...
public class ChildComponent{}
和配置class定义:
@ComponentScan(
value = "com.example",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.Child*")})
这样的配置,Spring根本找不到任何bean。
当星号不是用于匹配模式的最后部分而是用于两者之间的某处时,它似乎可以正常工作。
例如,以下配置可以毫无问题地匹配所有服务:
@ComponentScan(
value = "com.example",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.*Service")})
这是框架的设计行为还是应该可以使用这样的 'ant like' 正则表达式模式来匹配模式的最后部分?
类型 FilterType.REGEX
的过滤器使用标准 java Pattern
和 Matcher
进行匹配,因此不会匹配像 "com.example.*.Child*"
这样的类似蚂蚁的模式。 "com.example.*.*Service"
的唯一原因是 .*
匹配任何字符序列。为了 include/exlude 使用正则表达式,请使用有效的正则表达式。
编辑:
因此,在这种情况下,一种可能的选择是使用 com.example.child.Child.*
之类的模式来匹配 com.example.child
包中名称中以 Child 开头的任何 类。
我正在尝试使用 @ComponentScan
注释的 IncludeFilters 属性 来管理我的 spring 应用程序加载的 bean。我使用 FilterType.REGEX
类型的过滤器。我想匹配任何东西作为我模式的最后一部分使用但我似乎根本不那样工作。
我有一个 bean 定义:
package org.example.child;
public class ChildDao {}
...
public class ChildService{}
...
public class ChildComponent{}
和配置class定义:
@ComponentScan(
value = "com.example",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.Child*")})
这样的配置,Spring根本找不到任何bean。
当星号不是用于匹配模式的最后部分而是用于两者之间的某处时,它似乎可以正常工作。
例如,以下配置可以毫无问题地匹配所有服务:
@ComponentScan(
value = "com.example",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX,
pattern = "com.example.*.*Service")})
这是框架的设计行为还是应该可以使用这样的 'ant like' 正则表达式模式来匹配模式的最后部分?
类型 FilterType.REGEX
的过滤器使用标准 java Pattern
和 Matcher
进行匹配,因此不会匹配像 "com.example.*.Child*"
这样的类似蚂蚁的模式。 "com.example.*.*Service"
的唯一原因是 .*
匹配任何字符序列。为了 include/exlude 使用正则表达式,请使用有效的正则表达式。
编辑:
因此,在这种情况下,一种可能的选择是使用 com.example.child.Child.*
之类的模式来匹配 com.example.child
包中名称中以 Child 开头的任何 类。