【问题标题】:base-package in component-scan pick up the wrong subpackage组件扫描中的基础包选择了错误的子包
【发布时间】:2013-01-15 00:53:01
【问题描述】:

我已经把大部分课程放在了

  com.company.productline.product  -- classpath 1

在该类路径中会有服务、网络、域、i18n... 子包。

由于某种原因,我将另一个服务 bean 包装在一个罐子中,它应该适用于整个产品线,因此它在下面

  com.company.productline    -- classpath 2

所以在 applicationContext.xml 中,组件扫描的基本包必须妥协到上一级,作为类路径 2 而不是类路径 1,就像这样

  <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 中有一个错误说:

 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下没有class/classpath,但是com.company.productline.product.i18n.someServiceClass下有class。

  2. someServiceClass 类确实有一个@Component 注解。

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

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

类是这样定义的:

@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 上的 Spring 3.1.0 上运行

请帮忙,提前谢谢。

【问题讨论】:

  • 您能否确认您的 applicationContext 没有手动 bean 连接来寻找类 com.company.productline.i18n.someServiceClass?在你的整个项目中搜索那条线,看看它会产生什么。
  • 谢谢@ninn。我认为在这种情况下,类名应该出现在 xml 中,对吗?我进行了整个项目搜索,但没有发现类名出现在任何 xml 文件中,也没有出现在属性文件中。
  • 是的。这绝对很奇怪,我以前从未听说过发生任何事情。听起来您最近将“someServiceClass”移到了一个新包中,但您的包结构和服务器不知道它。尝试从它自己的服务器中清理您的服务(删除您的可部署战争、耳朵等)。然后进行完整的全新安装,我假设您使用 Maven,然后再次部署。
  • @ninn。谢谢你。首先,在我构建项目后,错误来自 Eclipse。然后我删除了vFabric服务器实例下的target文件夹和deployment文件夹,然后重建项目,但是没有用。
  • 我已经用类定义更新了问题,可能是类本身有问题。

标签: java spring jakarta-ee spring-mvc


【解决方案1】:

事实证明 ninn 是正确的。没有其他可能,只有在应用程序中有一个具有相同 bean 名称的类。

在这种情况下无法进行项目搜索的原因是我们有另一个具有相同服务类的 jar 文件。直到注意到我才知道。删除源代码中的类后,错误就消失了。

谢谢你。

【讨论】:

    猜你喜欢
    • 2016-10-21
    • 2018-07-22
    • 2013-06-02
    • 1970-01-01
    • 2015-04-25
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多