我认为@ComponentScan
能够以这种方式处理多个包:
@ComponentScan(basePackages = { "com.foo.bar.service", "com.foo.bar.repository" })
但是每当我将多个包添加到包数组中时,我都会遇到异常以及仅 com.foo.bar
使用时(我认为使用时com.foo.bar
,com.foo.bar
以及所有子包都会被扫描。)
我如何告诉 Spring 扫描所有提到的包或一个包及其子包?
例外:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'loginLogoutController':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.foo.bar.service.MyService com.foo.bar.controller.LoginLogoutController.myService;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'repositoryMyService':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.foo.bar.repository.MyRepository com.foo.bar.service.RepositoryMyService.myRepository;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.foo.bar.repository.MyRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
存储库类只是一个扩展接口JpaRepository
。
MyService
是一个像往常一样的接口,RepositoryMyService
实现MyService
并用@Service
. 用LoginLogoutController
注释@Controller
。