【问题标题】:How the directory structure for SpringBoot MongoDB CrudRepositoryCustom Implementation should be organized?Spring Boot MongoDB CrudRepository 自定义实现的目录结构应该如何组织?
【发布时间】:2019-02-06 10:59:20
【问题描述】:

我使用 SpringBoot 和 MongoDB 创建了一个简单的 maven 项目。我有两个存储库实现,即 StudentRepositoryStudentRepositoryCustomStudentRepository 扩展了内置的 MongoRepository 和自定义存储库。自定义存储库方法在 StudentRepositoryImpl 中实现。当我将 StudentRepositoryStudentRepositoryCustomStudentRepositoryImpl 放入同一个包(即 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


    【解决方案1】:

    我通过重构自定义存储库及其实现类的包结构解决了我的问题。我从这个问题中了解到的是 Spring 在子包中为存储库寻找实现的类。
    例如,如果存储库是在 com.foo.repo 中定义的,那么它的实现类应该在 com.foo.repo.impl 中。
    存储库应在顶级包中定义,实现类应在同一包中或其子包中。

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 2019-08-06
      • 2017-06-07
      • 2013-06-06
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多