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
?
@ComponentScan
或者更确切地说,ComponentScanAnnotationParser
它处理它,不支持pattern
值FilterType.ANNOTATION
.相反,只需提供一个适当类型@ComponentScan.Filter#value
的Class
值.
@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
.
归档时间: |
|
查看次数: |
4029 次 |
最近记录: |