我有下面的界面
public interface MyInterface<T, H extends Serializable, R extends Serializable> extends Repository<T, ID> {
default List<T> findAll(H key) {
return Collections.EMPTY_LIST;
}
}
实现如下,
public class RepositoryImpl<T, H extends Serializable, R extends Serializable>
extends ACConcreateClassWhichImplementRepository<T, H> implements MyInterface<T, H, R> {
@Override
public List<T> findAll(H key) {
return findSome(hashKey); //some method
}
}
通过控制器调用它时,调用击中MyInterface中定义的默认方法,而不是击中RepositoryImpl中的实现。
java.lang.Thread.State: RUNNABLE
at com.MyInterface.findAll(MyInterface.java:12)
at java.lang.invoke.LambdaForm$DMH.1294483354.invokeSpecialIFC(LambdaForm$DMH:-1)
at java.lang.invoke.LambdaForm$MH.995808242.invoke(LambdaForm$MH:-1)
at java.lang.invoke.LambdaForm$MH.1531802663.invoke(LambdaForm$MH:-1)
at java.lang.invoke.Invokers$Holder.invokeExact_MT(Invokers$Holder:-1)
at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:710)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:86)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy167.findAll(Unknown Source:-1)
而不是默认方法如果我在接口中定义了一个抽象方法,那么它就会击中实现。
at com.RepositoryImpl.findAll(RepositoryImpl.java:16)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker$$Lambda$1357.2068454901.invoke(Unknown Source:-1)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:639)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy167.findAll(Unknown Source:-1)
我使用,Spring版本5.3.21spring boot version 2.7.0
因此你是通过接口自动连接的,你需要在@Autowire注释的顶部添加@Qulifire注释,或者你需要通过实际实现的类自动连接。请参考此使用@Qualifire注释
https://www.baeldung.com/spring-qualifier-annotation