上下文中的多个包:组件扫描、spring config
如何在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
下面的方法是正确的:
注意,错误抱怨的是
xyzdao.daoservice.LoginDAO
,它不在上面提到的包中,也许你忘记添加它:The following approach is correct:
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:注释方法
Annotation Approach
您可以添加多个基础包(请参阅axtavt的答案 ),但您也可以 过滤基本包内扫描的内容:
You can add multiple base packages (see axtavt's answer), but you can also filter what's scanned inside the base package:
将会起作用,因为其余的包是“xyz”的子包。因此,您无需单独提及每个包。
will work since the rest of the packages are sub packages of "x.y.z". Thus, you dont need to mention each package individually.
另一种通用的注释方法:
Another general Annotation approach:
延迟响应,但要使用基于注释的方法提供多个包,我们可以使用如下所示:
@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"})
如果 xyz 是通用包,那么您可以使用:
它将包含所有以 xyz 开头的包,例如:
xyz控制器、xyz服务等
If x.y.z is the common package then you can use:
it will include all the package that is start with x.y.z like:
x.y.z.controller,x.y.z.service etc.
例如,您有包“com.abc”,并且里面有多个包,您可以使用像
For Example you have the package "com.abc" and you have multiple packages inside it, You can use like