UnsatisfiedDependencyException for customer method
在命令行中运行spring boot之后,出现以下错误:https://egkatzioura.com/2016/06/03/add-custom-functionality-to-a-spring-data-repository/
我的项目源代码在github上:https://github.com/b3nhysteria/exprole_spring
此问题正在申请中:
1
| System. out. println("============"+customerService. getListTransactionByCustomer()); |
服务中的实现为
1 2 3
| public String getListTransactionByCustomer (){
return customerRepository. getAllTransaction();
} |
在为method添加了自定义的repo实现后,即使只是为了返回消息而进行了更改,我仍然遇到问题。
如果有人有相同的建议,我会尝试的
顺便说一句,此脚本用于探索/学习。
在CustomerRepository中实现TransactionRepoCustom
1 2 3
| public interface CustomerRepository extends JpaRepository<Customer, CustomerId>, TransactionRepoCustom {
// ...
} |
因此,Spring尝试找到在TransactionRepoCustom中声明的方法public String getAllTransaction()的实现(但未这样做)。
因此,要解决此问题,您需要:
-
实现getAllTransaction;也许创建另一个类TransactionRepoCustomImpl(推荐!),或者
-
在接口中声明getAllTransaction的默认实现(如果使用Java 8)。
按照第一种方法,它将类似于:
1 2 3 4 5 6
| public class TransactionRepoCustomImpl implements TransactionRepoCustom {
@Override
public String getAllTransaction () {
return"logic for your custom implementation here";
}
} |
遵循最后一种方法是:
1 2 3 4 5
| public interface TransactionRepoCustom {
public default String getAllTransaction () {
return"Not implemented yet!"; // of course, this is just a naive implementation to state my point
}
} |
我已经创建了拉取请求,因此您可以在代码中尝试。
附加说明:
发生这种情况是因为Spring使用implements接口提供的CustomerRepository幕后默认(方法)实现。例如,使用Jparepository接口就是这种情况:您可以使用customerRepository.findAll(),并且没有在CustomerRepository中声明(也未实现)该方法(该方法取自Jparepository的实现) ,最有可能是为您打包的依赖项。)
现在,当您创建自己的接口(TransactionRepoCustom)并由CustomerRepository实现该接口时,Spring会尝试查找在TransactionRepoCustom中声明的所有方法的实现。由于您未提供任何内容,因此spring无法为您创建该bean。
在这种情况下,通过我提供的修复程序,Spring会找到该方法的实现,因此它可以为您创建bean。
最后,我说推荐的方法是为该方法提供一个实现,因为在Spring中做事情的方法是在接口中声明方法,并在其他类中提供其实现(该接口的默认实现)。在这种情况下,您可以为此创建一个单独的类,或在CustomerRepository中实现getAllTransaction方法。
当只有一个方法(可争论)时,可以在同一接口中声明一个default实现,但是如果接口变大,则可能很难维护。
- 哇,它的工作..但我不太明白它的背后,为什么我必须在我的界面中添加默认关键字?那么我是否要在TransactionRepoCustom接口中添加另一种方法呢?推荐呢?
-
@Benikurniawan请参阅我刚刚在答案中添加的其他说明部分。我希望我能解释得尽可能详细,并且没有遗漏任何东西:)
-
很好,我知道了,但是如果我需要一些自定义方法怎么办?喜欢getAllTransaction吗?如果我将其放在CustomerRepository中,该如何实现主体?很抱歉我缺乏知识
-
@Benikurniawan,我不好,您不能在CustomerRepository中实现该方法(不是不使用帖子中提到的default机制,这是一个接口!),您需要创建另一个实现TransactionRepoCustom的类...在这里实现您对getAllTransaction的逻辑。对不起,我错过了这个细节:(....我更新了帖子
-
我只是尝试遵循您的建议,但仍然出现错误,我将其推送到主分支中,希望您/任何人都可以帮助我
-
@Benikurniawan,您遇到的新问题是因为无效的方法名称getAllTransaction...。看到此内容,您将了解:stackoverflow.com/questions/51115098 / ... ...如果最终要解决,请告诉我解决它...如果您认为这变得越来越复杂,请在控制台上用在控制台中生成的完整日志在SO上发布另一个问题...。顺便说一句,这是与您在原始问题中提出的问题分开的问题问题....这就是为什么我建议您发布另一个问题
-
@Benikurniawan只需几句话....您在接口存储库中创建的方法必须对JPA有意义,以便让它知道应该为该方法为您创建什么查询...日志中的关键是这个部分:Failed to create query for method public abstract java.lang.String com.cart.repository.TransactionRepoCustom.getAllTransaction(??)! No property getAllTransaction found for type Customer!
-
顺便说一句,您尝试使用此方法获得什么:getAllTransaction?
-
我想创建一种方法来获取所有包含客户数据的交易,所以在我看来,我想用该方法创建包含包含订单明细数据的客户列表的哈希图,或者这不是Spring或逻辑等方面的最佳解决方案?任何帮助我更好的建议,我将不胜感激,顺便说一句,我遵循您的建议,但没有,但仍然有错误的bean名称