做上下文:组件扫描编程方式? [英] Doing context:component-scan programatic way?
问题描述
我目前混合使用 AnnotationConfigApplicationContext
和 ClasspathXmlApplicationContext
,并将 AnnotationConfigApplicationContext
作为父上下文.但是我发现 AnnotationConfigApplicationContext
中定义的 bean 不能很好地处理 ClasspathXmlApplicationContext
中定义的 bean.所以我无论如何都想放弃 ClasspathXmlApplicationContext
,让我的应用程序只使用 AnnotationConfigApplicationContext
.
I'm mixed using AnnotationConfigApplicationContext
and ClasspathXmlApplicationContext
currently, and make AnnotationConfigApplicationContext
as the parent context. But I found that beans defined in AnnotationConfigApplicationContext
doesn't cope well with beans defined in ClasspathXmlApplicationContext
. So I'd like to drop ClasspathXmlApplicationContext
anyway, and make my application use AnnotationConfigApplicationContext
only.
问题是,我不知道如何完全替换
.我可以使用 AnnotationConfigApplicationContext.scan(...)
轻松进行包扫描,但似乎无法在 AnnotationConfigApplicationContext
中添加包含/排除模式.
The problem is, I don't know how to replace <context:component-scan>
totally. I can easily do a package scan using AnnotationConfigApplicationContext.scan(...)
, but there seems no way to add include/exclude pattern in AnnotationConfigApplicationContext
.
有什么想法吗?
推荐答案
它看起来不像类 AnnotationConfigApplicationContext 提供排除/包含过滤器盒子外面.该类在内部使用 ClassPathBeanDefinitionScanner的实例a> 扫描提供方法 addExcludeFilter
和 addIncludeFilter
的注释.不幸的是,该字段是 private
并且没有 getter 方法,因此您不能只编写扩展 AnnotationConfigApplicationContext
并添加包含和排除方法的实现.相反,您可能必须从 AnnotationConfigApplicationContext
复制代码并添加缺少的方法:
It doesn't look like the class AnnotationConfigApplicationContext provides exclusion/inclusion filters out-of-the-box. Internally the class uses an instance of ClassPathBeanDefinitionScanner to scan for annotations which provides the methods addExcludeFilter
and addIncludeFilter
. Unfortunately, the field is private
and doesn't have a getter method so you can't just write an implementation that extends AnnotationConfigApplicationContext
and add include and exclude methods. Instead you'll 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);
}
这篇关于做上下文:组件扫描编程方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!