Spring中FilterType.ANNOTATION的字符串模式

Cra*_*der 2 java spring spring-mvc spring-java-config

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

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

但似乎没有工作,并给我一个例外说

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
Run Code Online (Sandbox Code Playgroud)


我已经看过这篇文章,但它并不满足@Controller定义的排除过滤器

pattern = "org.springframework.stereotype.Controller")
Run Code Online (Sandbox Code Playgroud)


我该怎么写字符串模式FilterType.ANNOTATION

Sot*_*lis 5

@ComponentScan或者更确切地说,ComponentScanAnnotationParser它处理它,不支持patternFilterType.ANNOTATION.相反,只需提供一个适当类型@ComponentScan.Filter#valueClass值.

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

在评论中,你说

我想要排除包含控制器的包

我不知道从组件扫描中排除整个包的方法,因为它包含一个带@Controller注释的类型.但是,如果您要做的只是@Controller从指定的包中排除包,那么上面的方法就是这样做.

例如

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 {
}
Run Code Online (Sandbox Code Playgroud)

打印

{}
Run Code Online (Sandbox Code Playgroud)

一个空的Map归来getBeansWithAnnotation.