我认为@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
。