使用SessionCallback泛型方法无法使用Lambda表达
@Test
void contextLoads() {
User user = new User("小明",20);
SessionCallback<User> callback = new SessionCallback<User>() {
@Override
public User execute(RedisOperations redisOperations) throws DataAccessException {
redisOperations.boundValueOps("user").set(user);
return (User) redisOperations.boundValueOps("user").get();
}
};
User execute = (User)redisTemplate.execute(callback);
System.out.println(execute);
}
@Test
void testLamdba(){
// User user = new User("小明",20);
// SessionCallback callback = (SessionCallback)(RedisOperations redisOperations)->{
// redisOperations.boundValueOps("user").set(user);
// return (User) redisOperations.boundValueOps("user").get();
// };
}
使用匿名函数可以正常使用,但是使用Lambda表达式则会报错Target method is generic
,原因在于:Lambda表达式是一种函数式编程,作用就是重写接口的唯一方法,也可以看做是声明这个方法,而泛型方法中的类型参数,是在声明方法的时候使用的,而只有在调用方法的时候才确定具体的类型,目前Lambda语法是没办法兼有泛型方法的类型参数,