gpt4 book ai didi

java - Spring 无法使用 @ComponentScan 中带有星号的 REGEX 类型过滤器来包含/排除 Bean

转载 作者:太空宇宙 更新时间:2023-11-04 09:23:14 30 4
gpt4 key购买 nike

我正在尝试使用 @ComponentScan 注释的 IncludeFilters 属性来管理我的 Spring 应用程序加载的 bean。我使用 FilterType.REGEX 类型的过滤器。我想使用任何东西作为我的模式的最后一部分,但我似乎根本不这样做。我有一个 bean 定义:

package org.example.child;

public class ChildDao {}
...
public class ChildService{}
...
public class ChildComponent{}

和配置类定义:

@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 ”的正则表达式模式来匹配模式的最后部分?

最佳答案

FilterType.REGEX 类型的过滤器使用标准 java PatternMatcher 进行匹配,因此不会匹配像 "com.example.*.Child*" 这样的类似 Ant 的模式。 "com.example.*.*Service" 的唯一原因是 .* 匹配任何字符序列。为了使用正则表达式包含/排除,请使用有效的正则表达式。

编辑:

因此,在这种情况下,一种可能的选择是使用 com.example.child.Child.* 之类的模式来匹配 com.example.child 包中名称中以 Child 开头的任何类。

关于java - Spring 无法使用 @ComponentScan 中带有星号的 REGEX 类型过滤器来包含/排除 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58051600/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com