Context: software-software path?

I am mixed using AnnotationConfigApplicationContext and ClasspathXmlApplicationContext at the moment, and create AnnotationConfigApplicationContext as the parent context. But I found that beans, defined in AnnotationConfigApplicationContext , do a poor job with beans, defined in ClasspathXmlApplicationContext . So I would like to leave ClasspathXmlApplicationContext anyway and use only my AnnotationConfigApplicationContext application.

The problem is that I do not know how to completely replace <context:component-scan> . I can easily scan packets using AnnotationConfigApplicationContext.scan(...) , but there seems to be no way to add an include / exclude pattern to AnnotationConfigApplicationContext .

Any idea?

+6
java spring annotations
source share
1 answer

It does not seem that the AnnotationConfigApplicationContext class provided exclusion / inclusion filters from the box. Internally, the class uses an instance of ClassPathBeanDefinitionScanner to scan annotations, which provides the addExcludeFilter and addIncludeFilter . Unfortunately, the field is private and does not have a getter method, so you can simply write an implementation that extends AnnotationConfigApplicationContext and add include and exclude methods. Instead, you probably have to copy the code from AnnotationConfigApplicationContext and add the missing methods:

 public void addExcludeFilter(TypeFilter excludeFilter) { this.scanner.addExcludeFilter(excludeFilter); } public void addIncludeFilter(TypeFilter includeFilter) { this.scanner.addIncludeFilter(includeFilter); } 
+5
source share

All Articles