I'm using a Java based configuration class to develop a Spring MVC app. I want to add a filter for my Controller classes in @ComponentScan like this:
@Configuration
@ComponentScan(basePackages = { "org.fandom.configclass" },
excludeFilters = { @ComponentScan.Filter (type = FilterType.ANNOTATION,
pattern = "org.springframework.stereotype.Controller")})
public class Config {
// some stuff
}
but it seems not to work and gives me an exception saying
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
I've seen this post, but it is not satisfying the exclude filter on @Controller as defined
pattern = "org.springframework.stereotype.Controller")
How can I write a string pattern for FilterType.ANNOTATION?
@ComponentScan, or rather the ComponentScanAnnotationParser that processes it, does not support a pattern value with FilterType.ANNOTATION. Instead, just provide a @ComponentScan.Filter#value with a Class value of the appropriate type.
@ComponentScan(
basePackages = { "org.fandom.configclass" },
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
}
)
In a comment, you state
I want to exclude a package containing Controllers
I don't know of a way to exclude an entire package from component scanning just because it includes a single @Controller annotated type. However, if all you want to do is to exclude @Controller packages from a specified package, then the above is the way to do it.
For example
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 {
}
which prints
{}
an empty Map returned by getBeansWithAnnotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With