这样做的上下文:组件扫描程序化的方式? [英] Doing context:component-scan programatic way?

查看:155
本文介绍了这样做的上下文:组件扫描程序化的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是混合使用 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.

问题是,我不知道如何更换&LT;背景:组件扫描&GT; 完全。我可以用 AnnotationConfigApplicationContext.scan(...)很容易做到包扫描,但似乎没有办法添加中包括/ AnnotationConfigApplicationContext <排除模式/ code>。

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.

任何想法?

推荐答案

它看起来并不像类<一个href=\"http://www.jarvana.com/jarvana/view/org/springframework/spring-context/3.0.5.RELEASE/spring-context-3.0.5.RELEASE-sources.jar!/org/springframework/context/annotation/AnnotationConfigApplicationContext.java?format=ok\">AnnotationConfigApplicationContext提供排除/包含过滤器外的开箱。在内部类使用的实例<一个href=\"http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.html\">ClassPathBeanDefinitionScanner扫描它提供了注解的方法 addExcludeFilter addIncludeFilter 。不幸的是,本场私人并没有一个getter方法​​,所以你不能只写一个扩展实施 AnnotationConfigApplicationContext 并添加包括和排除方法。相反,你可能会需要从 AnnotationConfigApplicationContext 复制code和添加缺少的方法:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆