首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组件中的基包-扫描选择错误的子包

组件中的基包-扫描选择错误的子包
EN

Stack Overflow用户
提问于 2013-01-15 00:53:01
回答 1查看 2K关注 0票数 0

我把班上的大多数人都关进了

代码语言:javascript
复制
  com.company.productline.product  -- classpath 1

在这类路径中将有服务、web、域、i18n.子包。

由于某些原因,我在jar中包装了另一个服务bean,它应该可以用于整个产品线,因此它位于

代码语言:javascript
复制
  com.company.productline    -- classpath 2

因此在applicationContext.xml中,组件扫描的基本包必须妥协到一个级别上,作为类路径2而不是类路径1,如下所示

代码语言:javascript
复制
  <context:component-scan base-package="com.company.productline">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
  </context:component-scan>

然后让Spring在整个应用程序中扫描@Service或@Component,甚至在jar文件中。

但是,现在在applicationContext中有一个错误:

代码语言:javascript
复制
 Annotation-specified bean name 'someServiceClass' for bean class
 [com.company.productline.i18n.someServiceClass] conflicts with existing, 
 non-compatible bean definition of same name and class 
[com.company.productline.product.i18n.someServiceClass]'

问题是Spring似乎在一个错误包com.company.productline.i18n.someServiceClass下找到了一个bean类,其中没有product,但是我可以确认如下:

  1. com.company.productline.i18n.someServiceClass下没有类/类路径,但是在com.company.productline.product.i18n.someServiceClass下有一个类。
  2. someServiceClass确实有一个@Component注释。

但是,如果我将base-package中的类路径降低一个级别,错误就消失了:

代码语言:javascript
复制
  <context:component-scan base-package="com.company.productline.product">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
  </context:component-scan>

类的定义如下:

代码语言:javascript
复制
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "request")
public class SomeServiceClass implements CurrentRequest {

    @Autowired
    private HttpServletRequest request;

    public Locale getCurrentLocale() {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        return localeResolver.resolveLocale(request);
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

}

所以不知道到底发生了什么,为什么会有这个问题。

该应用程序运行在STS 2.9.1上的Spring3.1.0上

请帮忙,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-15 15:24:27

原来宁恩是对的。没有其他的可能,只有应用程序中的类具有相同的bean名称。

在本例中,项目搜索不起作用的原因是我们有另一个具有相同服务类的jar文件。直到被注意到我才知道。在删除源代码中的类之后,错误就消失了。

谢谢尼恩。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14329477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档