[英]Injecting custom repository implementation in a service layer
I am written a custom JPA implementation using the Spring JPA documentation as reference, and I wish to inject aforementioned implementation in a service layer, but I can not figure out what I am doing wrong. 我使用Spring JPA文档作为参考编写了一个自定义JPA实现,并且希望将上述实现注入服务层,但是我无法弄清楚自己在做什么错。 I have also gone through the following documentation as well .
我也阅读了以下文档 。 My application is a Spring boot application.
我的应用程序是一个Spring Boot应用程序。
The documentation does not say how to initialise my custom repository implementation. 该文档没有说明如何初始化我的自定义存储库实现。 I have tried to follow the following instructions .
我试图按照以下说明进行操作 。
My Service layer. 我的服务层。
@Service
public class ProfileService implements IProfileService {
@Autowired
private IQuizRepository quizRepository;
. . . . Some methods defined here. . . .
}
My JPA interface 我的JPA界面
@NoRepositoryBean
public interface IQuizRepository extends JpaRepository<Quiz, Long> {
Quiz registerQuiz();
}
Concrete class 具体课
public class QuizRepository extends SimpleJpaRepository<Quiz, Long> implements IQuizRepository {
@PersistenceContext
private EntityManager em;
public QuizRepository(Class<Quiz> clazz, EntityManager em) {
super(clazz, em);
}
/**
* Initialises and returns a quiz attempt.
*/
@Override
public Quiz registerQuiz() {
String uniqueKey = UUID.randomUUID().toString().replaceAll("-", "");
Quiz quiz = new Quiz();
quiz.setQuizId(uniqueKey);
Score score = new Score();
quiz.setScore(score);
score.setQuiz(quiz);
this.save(quiz);
return quiz;
}
}
When I don't annotate QuizRepository
concrete class with Repository
, then I get the following error. 当我不使用
Repository
注释QuizRepository
具体类时,就会出现以下错误。
***************************
APPLICATION FAILED TO START
***************************
Description:
Field quizRepository in com.myproject.service.ProfileService required a bean of type 'com.myproject.repository.IQuizRepository' that could not be found.
Action:
Consider defining a bean of type 'com.myproject.repository.IQuizRepository' in your configuration.
But when I annotate QuizRepository
with @Repository
, then I get the following error 但是,当我注释
QuizRepository
与@Repository
,然后我得到以下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.myproject.repository.QuizRepository required a bean of type 'java.lang.Class' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Class' in your configuration.
My project has a combination of JPA interfaces inheriting from JPA repository, and concrete implementations. 我的项目结合了从JPA存储库继承的JPA接口和具体的实现。
Sounds like your constructor needs to have these exact two parameters: 听起来您的构造函数需要具有这些确切的两个参数:
public QuizRepository(JpaEntityInformation<Quiz, Long> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
instead of public QuizRepository(Class<Quiz> clazz, EntityManager entityManager)
而不是
public QuizRepository(Class<Quiz> clazz, EntityManager entityManager)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.