Spring mvc @ComponentScan在Spring3.2中是如何工作的?
我认为Spring mvc @ComponentScan在Spring3.2中是如何工作的?,spring-mvc,annotations,autowired,Spring Mvc,Annotations,Autowired,我认为@ComponentScan能够以这种方式处理多个包: @ComponentScan(basePackages = { "com.foo.bar.service", "com.foo.bar.repository" }) 但是,每当我在包数组中添加多个包时,我都会得到一个异常,当我只使用com.foo.bar时也是如此(我认为当使用com.foo.bar时,com.foo.bar以及所有子包都会被扫描) 我如何告诉Spring扫描所有提到的包或包及其子包 例外情况: org.spring
@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)}
repository类只是一个扩展JpaRepository
的接口。
MyService
像往常一样是一个接口,RepositoryMyService
实现MyService
,并用@Service
注释。LoginLogoutController
带有@Controller
注释,您应该能够执行您所说的操作(只需指定父包)。你能展示更多包含@ComponentScan
注释的类吗?你启用了JPA存储库配置了吗?这就是我在此期间所做的工作:@EnableJpaRepositories