【发布时间】:2019-02-06 10:59:20
【问题描述】:
我使用 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);
}
}
这里是 StudentRepository:
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,应用程序工作正常。
任何帮助将不胜感激!!!谢谢。
【问题讨论】:
标签: java mongodb spring-boot directory