Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

是否支持同一个类内部不同方法调用时也走缓存 #78

Open
zangyk opened this issue May 25, 2018 · 10 comments
Open

是否支持同一个类内部不同方法调用时也走缓存 #78

zangyk opened this issue May 25, 2018 · 10 comments
Labels

Comments

@zangyk
Copy link

zangyk commented May 25, 2018

classe A{

method_1 ( ){
return method_2 ( );
}
@cached
method_2 ( ){
}
}

spring 原有的cache模块可以用AspectJ+LTW实现类内部方法相互调用也走缓存。jetcache是否支持?

@areyouok
Copy link
Collaborator

可以加,实现的时间不能保证

@linda8167
Copy link

同求额。。。

@calebzhao
Copy link

同求

@coolyujiyu
Copy link

你可以把A类注入到A类,然后A.method_2 ( )就可以了啊
起码要走一下Spring的AOP

@jayknoxqu
Copy link

jayknoxqu commented Oct 17, 2018

在SpringBoot的启动类增加注解:

@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

A接口

public interface A{

    @Cached
    String method_1();

    String method_2();

}

B类

@Service
public class B implements A{
    
    @Override
    public String method_1(){
        return  "foo";
    }

    @Override
    public String method_2(){
         //获取代理对象
        A a= (A) AopContext.currentProxy();
        return a.method_1();
    }

}

这样就可以实现同类之间调用也走缓存

@ghost
Copy link

ghost commented Nov 7, 2018

请问这个TODO目前有没有计划?
这个需求比较强烈,有点想加入实现这个功能。

@areyouok
Copy link
Collaborator

areyouok commented Nov 7, 2018

现在有点不想做了。

如果this调用也走缓存需要引入AspectJ,AspectJ比较小众,配置很复杂,估计很多人都搞不定。比如AspectJ和Spring AOP混用时要特别指定哪些Bean走Spring AOP哪些走AspectJ,为了织入AspectJ的Aspect需要对编译后的字节码进行加强处理,或者搞LTW,还有额外的配置文件。

现在想要同类调用走缓存有个简单的办法,在bean中通过Autowired注解注入自己,然后使用注入的实例(而不是this)来调用方法,就可以了。

@CodingOX
Copy link

现在有点不想做了。

如果this调用也走缓存需要引入AspectJ,AspectJ比较小众,配置很复杂,估计很多人都搞不定。比如AspectJ和Spring AOP混用时要特别指定哪些Bean走Spring AOP哪些走AspectJ,为了织入AspectJ的Aspect需要对编译后的字节码进行加强处理,或者搞LTW,还有额外的配置文件。

现在想要同类调用走缓存有个简单的办法,在bean中通过Autowired注解注入自己,然后使用注入的实例(而不是this)来调用方法,就可以了。

楼上有位大哥提到了注解exposeProxy = true 可以处理issue中提出的问题,根据我的经验,应该是可以的。他的做法没有引入 AspectJ吧?这种做法会带来负面影响?

@godricooxx
Copy link

这种是常见的 spring aop 嵌套调用时内部aop失效的问题

问题原因:在使用 Spring AOP 的时候,我们从 IOC 容器中获取的 Bean 对象其实都是代理对象,而不是那些 Bean 对象本身,而当在自己的 Service 类中使用 this 关键字嵌套调用同类中的其他方法时,由于 this 关键字引用的并不是该 Service Bean 对象的代理对象,而是其本身,故 Spring AOP 是不能拦截到这些被嵌套调用的方法的。

下面是2种解决方法:1、类自我注入,使用@lazy@Autowired注解实现自我注入,然后使用时用注解的实例代替this调用方法。
2、暴露Aop代理到ThreadLocal支持,在类之前加@EnableAspectJAutoProxy(exposeProxy = true),调用方法时使用((XxxService) AopContext.currentProxy()).method()调用方法,也就是楼上某位同学提到的。

@rw12306
Copy link

rw12306 commented Jan 11, 2021

我就是controller 层 调service 层,这样也走不到缓存,以前还是好好的,今天突然来测试发现这类里面的所有方法缓存都不起作用了,其他地方的方法缓存可以用,这是遇到的一个大坑,不晓得什么原因造成的。哪位大神知道怎么解决,请联系qq:493966729
----------------controller 层---------------
public class ServiceWsController {
@Autowired
private SerSraAppService serSraAppService;
@ApiOperation(value = "效验key 开发:renwei")
@RequestMapping(value = "/findByKeyAndSerWsid", method = RequestMethod.POST)
public ResponseModel findByKeyAndSerWsid(String key, String sraAppid) {
return ResultUtils.me.success(serSraAppService.getBySerWsid(key,sraAppid));
}
----------------service 层---------------
@service("serSraAppService")
public class SerSraAppServiceImpl extends ServiceImpl<SerSraAppDao, SerSraAppPO> implements SerSraAppService {

@Override
@Cacheable(cacheManager = RedisConfig.REDIS_MANAGER,
        cacheNames = YbhRedisKeyConstant.FINDBYKEYANDSERWSID1
        ,key = "#key+':'+#serWsid")
public SerSraAppDetailVO getBySerWsid(String key,String serWsid){
    SerSraAppDetailVO vo = serSraAppDaoExt.findByKeyAndSerWsid(key, serWsid);
    return vo;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants