上下文中的多个包:组件扫描,弹簧配置

2022-08-31 06:24:35

如何在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:

答案 1

以下方法是正确的:

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

请注意,错误抱怨 ,这不在上面提到的包中,也许您忘记添加它:x.y.z.dao.daoservice.LoginDAO

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

答案 2

注释方法

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

推荐