仅实现 JpaSpecificationExecutor 的存储库会在启动 Spring boot 应用程序时导致错误
我们在启动 Spring Boot 应用程序时遇到了问题。我们添加了一个新的 API,其中包含一个控制器、一个服务类和两个表的两个存储库类。这是主表存储库
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import com.sixdee.wallet.core.domain.PromoTicketMaster;
@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster> {
}
我们查看了这个答案 Spring Data JPA 中的 CrudRepository 和 JpaRepository 接口有什么区别?在创建此存储库之前,因为它也用于分页方法。上面说,由于上面提到的继承,JpaRepository将拥有CrudRepository和PagingAndSortingRepository的所有功能。但应用程序并未使用此 Repo 配置启动,并且控制台显示此错误。自动装配很好。
*************************** APPLICATION FAILED TO START
***************************
Description:
Field promoTicketMasterRepo in com.sixdee.wallet.core.service.impl.PromoTicketServiceImpl required a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' in your configuration.
然后我们像这样更改了Repo并启动了应用程序。
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.sixdee.wallet.core.domain.PromoTicketMaster;
@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster>, CrudRepository<PromoTicketMaster, String> {
}
第一种方法有什么问题
we were facing issues while starting our Spring Boot Application. We added a new API with a Controller, a Service class, and two Repository classes for two tables. This is the master table repository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import com.sixdee.wallet.core.domain.PromoTicketMaster;
@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster> {
}
We had a look at this answer What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA? before creating this Repo as it was used for Pagination methods as well. It said that Because of the inheritance mentioned above, JpaRepository will have all the functions of CrudRepository and PagingAndSortingRepository . But the application didn't start with this Repo configuration and the console showed this error. The autowiring was fine.
*************************** APPLICATION FAILED TO START
***************************
Description:
Field promoTicketMasterRepo in com.sixdee.wallet.core.service.impl.PromoTicketServiceImpl required a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.sixdee.wallet.core.repository.PromoTicketMasterRepo' in your configuration.
Then we changed the Repo like this and the application was started.
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.sixdee.wallet.core.domain.PromoTicketMaster;
@Repository
public interface PromoTicketMasterRepo extends JpaSpecificationExecutor<PromoTicketMaster>, CrudRepository<PromoTicketMaster, String> {
}
What was wrong with the first approach
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论