java.lang.ClassCastException:无法强制转换为org.springframework.util.concurrent.ListenableFuture

时间:2015-10-12 04:50:59

标签: java asynchronous spring-boot spring-data future

我试图通过Spring Data jpa运行带有hibernate的Spring Boot应用程序。我有一个“用户”实体,

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public long id;

    @Column(name = "name", nullable = false)
    public String name = "";
}

和一个异步“UserRepository”,它扩展了Spring Data“Repository”,

public interface UserRepository extends Repository<User, Long> {
    @Async
    ListenableFuture<User> save(User user);
}

但是,当调用“save”时,

ListenableFuture<User> futureCreated = userRepository.save(newUser);
                    futureCreated.addCallback(created -> {
                               // do something 
                            },
                            err -> {
                               // do something else
                            });

我收到了以下错误,

java.lang.ClassCastException: com.***.User cannot be cast to org.springframework.util.concurrent.ListenableFuture
    at com.sun.proxy.$Proxy51.save(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:110)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:745)

我对应用程序的注释,

@SpringBootApplication
@EnableAsync
public class Application {
    // main
}

有什么想法吗?真的很感谢你的帮助!

0 个答案:

没有答案