gpt4 book ai didi

java - Spring 中 FilterType.ANNOTATION 的字符串模式

转载 作者:行者123 更新时间:2023-11-30 06:12:35 25 4
gpt4 key购买 nike

我正在使用基于 Java 的配置类来开发 Spring MVC 应用程序。我想在 @ComponentScan 中为我的 Controller 类添加一个过滤器,如下所示:

@Configuration
@ComponentScan(basePackages = { "org.fandom.configclass" },
excludeFilters = { @ComponentScan.Filter (type = FilterType.ANNOTATION,
pattern = "org.springframework.stereotype.Controller")})
public class Config {
// some stuff
}

但它似乎不起作用并且给了我一个异常(exception)说

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [org.fandom.configclass.Config]; nested exception is java.lang.IllegalArgumentException: Filter type not supported with String pattern: ANNOTATION
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:179)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5014)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:717)
Caused by: java.lang.IllegalArgumentException: Filter type not supported with String pattern: ANNOTATION
at org.springframework.context.annotation.ComponentScanAnnotationParser.typeFiltersFor(ComponentScanAnnotationParser.java:178)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:107)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:265)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:229)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
... 19 more


我看过this post ,但它不满足 @Controller 上定义的排除过滤器

pattern = "org.springframework.stereotype.Controller")


如何为 FilterType.ANNOTATION 编写字符串模式?

最佳答案

@ComponentScan ,或者说 ComponentScanAnnotationParser处理它的,不支持 patternFilterType.ANNOTATION .相反,只需提供 @ComponentScan.Filter#valueClass适当类型的值。

@ComponentScan(
basePackages = { "org.fandom.configclass" },
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
}
)

在评论中,你声明

I want to exclude a package containing Controllers

我不知道有什么方法可以将整个包排除在组件扫描之外,仅仅因为它包含一个 @Controller。注释类型。但是,如果您只想排除 @Controller packages from a specified package,那么上面就是这样做的方法。

例如

package com.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@Controller
public class Sample {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
System.out.println(ctx.getBeansWithAnnotation(Controller.class));
}
}

@Configuration
@ComponentScan(basePackages = { "com.example" },
excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class) })
class Config {
}

打印

{}

一个空的 MapgetBeansWithAnnotation 返回.

关于java - Spring 中 FilterType.ANNOTATION 的字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707404/

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