我使用SpringBoot和MongoDB创建了一个简单的maven项目。我有两个存储库实现,即 StudentRepository 和 StudentRepositoryCustom 。 StudentRepository 扩展了内置的 MongoRepository 和自定义存储库。自定义存储库方法在 StudentRepositoryImpl 中实现。当我将 StudentRepository , StudentRepositoryCustom 和 StudentRepositoryImpl 放在同一软件包(即 com.aman.springboot.repository )中时,应用程序运行没有错误。强>。但是,当将实现类移动到其他软件包时,例如 com.aman.springboot.impl 。
我在做什么错了?
这是主要班级:
package com.aman.springboot.client;
@SpringBootApplication(scanBasePackages = "com.aman.springboot")
public class ApplicationLauncher {
public static void main(String[] args) {
SpringApplication.run(StudentController.class, args);
}
}
这是RestController类:
package com.aman.springboot.controller;
@RestController
@EnableAutoConfiguration
@EnableMongoRepositories(basePackages = "com.aman.springboot.repository")
@RequestMapping(value = "/student")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@RequestMapping(value = "/getStudent", method = RequestMethod.GET)
public StudentRepo getStudent(@RequestParam(required = true) int id) {
return studentRepository.findStudentById(id);
}
@RequestMapping(value = "/removeStudent", method = RequestMethod.POST)
public void removeStudent(@RequestBody(required = true) StudentRepo
studentRepo) {
studentRepository.deleteStudent(studentRepo);
}
}
这里的学生资料库:
package com.aman.springboot.repository;
@Repository
public interface StudentRepository extends MongoRepository<StudentRepo,
String>, StudentRepositoryCustom {
public StudentRepo findStudentById(int id);
}
这里的StudentRepositoryCustom:
package com.aman.springboot.repository;
public interface StudentRepositoryCustom {
public void deleteStudent(StudentRepo studentRepo);
}
这是StudentRepositoryImpl:
package com.aman.springboot.impl;
@Service
public class StudentRepositoryImpl implements StudentRepositoryCustom{
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private StudentRepo student;
@Override
public void deleteStudent(StudentRepo studentRepo) {
mongoTemplate.remove(studentRepo);
}
}
您可以看到接口或存储库都在同一包中,但StudentRepositoryCustom接口的实现类在不同的包中。在这种情况下,应用程序在部署时会引发错误:
这是堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'studentController': Unsatisfied dependency
expressed through field 'studentRepository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'studentRepository': Invocation of init method failed; nested
exception is org.springframework.data.mapping.PropertyReferenceException: No
property deleteStudent found for type StudentRepo! at
org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
(AutowiredAnnotationBeanPostProcessor.java:586)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject
(InjectionMetadata.java:91) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues
(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-
5.0.8.RELEASE.jar:5.0.8.RELEASE] at
.
.
.
.
org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at
com.aman.springboot.client.ApplicationLauncher.main
(ApplicationLauncher.java:17) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'studentRepository': Invocation of init method
failed; nested exception is
org.springframework.data.mapping.PropertyReferenceException: No property
deleteStudent found for type StudentRepo! at
org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.initializeBean
(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-
5.0.8.RELEASE.jar:5.0.8.RELEASE] at
.
.
.
.
tializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-
5.0.8.RELEASE.jar:5.0.8.RELEASE] ... 29 common frames omitted
如果我将StudentRepositoryImpl类移动到存储库为即 com.aman.springboot.repository 的程序包中,则该应用程序将正常运行。
任何帮助将不胜感激!!!谢谢。
答案 0 :(得分:0)
我通过重构自定义存储库及其实现类的包结构解决了我的问题。我从问题中了解到的是,Spring在子包中寻找用于存储库的实现的类。
例如,如果存储库是在 com.foo.repo 中定义的,则其实现类应该在 com.foo.repo.impl 中。
存储库应在顶级程序包中定义,实现类应在同一程序包或其子程序包中。