上下文中的多个包:组件扫描、spring config

发布于 2024-10-21 13:05:43 字数 677 浏览 6 评论 0原文

如何在 spring-servlet.xml 文件的 context:component-scan 元素中添加多个包?

我已经尝试过

<context:component-scan base-package="z.y.z.service" base-package="x.y.z.controller" />

<context:component-scan base-package="x.y.z.service, x.y.z.controller" />

<context:component-scan base-package="x.y.z.service" />
<context:component-scan base-package="x.y.z.controller" />

出现错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [x.y.z.dao.daoservice.LoginDAO] found for dependency:

How can I add multiple packages in spring-servlet.xml file in context:component-scan element?

I have tried

<context:component-scan base-package="z.y.z.service" base-package="x.y.z.controller" />

and

<context:component-scan base-package="x.y.z.service, x.y.z.controller" />

and

<context:component-scan base-package="x.y.z.service" />
<context:component-scan base-package="x.y.z.controller" />

but got error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [x.y.z.dao.daoservice.LoginDAO] found for dependency:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

枕头说它不想醒 2024-10-28 13:05:43

下面的方法是正确的:

<context:component-scan base-package="x.y.z.service, x.y.z.controller" /> 

注意,错误抱怨的是xyzdao.daoservice.LoginDAO,它不在上面提到的包中,也许你忘记添加它:

<context:component-scan base-package="x.y.z.service, x.y.z.controller, x.y.z.dao" /> 

The following approach is correct:

<context:component-scan base-package="x.y.z.service, x.y.z.controller" /> 

Note that the error complains about x.y.z.dao.daoservice.LoginDAO, which is not in the packages mentioned above, perhaps you forgot to add it:

<context:component-scan base-package="x.y.z.service, x.y.z.controller, x.y.z.dao" /> 
孤城病女 2024-10-28 13:05:43

注释方法

@ComponentScan({ "x.y.z", "x.y.z.dao" })

Annotation Approach

@ComponentScan({ "x.y.z", "x.y.z.dao" })
荒岛晴空 2024-10-28 13:05:43

您可以添加多个基础包(请参阅axtavt的答案 ),但您也可以 过滤基本包内扫描的内容

<context:component-scan base-package="x.y.z">
   <context:include-filter type="regex" expression="(service|controller)\..*"/>
</context:component-scan>

You can add multiple base packages (see axtavt's answer), but you can also filter what's scanned inside the base package:

<context:component-scan base-package="x.y.z">
   <context:include-filter type="regex" expression="(service|controller)\..*"/>
</context:component-scan>
终难愈 2024-10-28 13:05:43
<context:component-scan base-package="x.y.z"/>

将会起作用,因为其余的包是“xyz”的子包。因此,您无需单独提及每个包。

<context:component-scan base-package="x.y.z"/>

will work since the rest of the packages are sub packages of "x.y.z". Thus, you dont need to mention each package individually.

红衣飘飘貌似仙 2024-10-28 13:05:43

另一种通用的注释方法:

@ComponentScan(basePackages = {"x.y.z"})

Another general Annotation approach:

@ComponentScan(basePackages = {"x.y.z"})
避讳 2024-10-28 13:05:43

延迟响应,但要使用基于注释的方法提供多个包,我们可以使用如下所示:

@ComponentScan({"com.my.package.one","com.my.package.subpackage .two","com.your.package.supersubpackage.two"})

A delayed response but to give multiple packages using annotation based approach we can use as below:

@ComponentScan({"com.my.package.one","com.my.package.subpackage.two","com.your.package.supersubpackage.two"})

狼性发作 2024-10-28 13:05:43

如果 xyz 是通用包,那么您可以使用:

<context:component-scan base-package="x.y.z.*">

它将包含所有以 xyz 开头的包,例如:
xyz控制器、xyz服务等

If x.y.z is the common package then you can use:

<context:component-scan base-package="x.y.z.*">

it will include all the package that is start with x.y.z like:
x.y.z.controller,x.y.z.service etc.

风透绣罗衣 2024-10-28 13:05:43

例如,您有包“com.abc”,并且里面有多个包,您可以使用像

@ComponentScan("com.abc")

For Example you have the package "com.abc" and you have multiple packages inside it, You can use like

@ComponentScan("com.abc")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文