spring aop如何改变返回值 [英] How to change the return value by spring aop

查看:186
本文介绍了spring aop如何改变返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DAO层有一个有返回值的方法,我想通过spring AOP改变返回值,根据不同的需求,然后发送到SERVICE层对应的方法;但我不知道该怎么做.

I have a method with a return value in DAO layer, I want to change the return value by spring AOP, according different requirement,s and then send to corresponding method in SERVICE layer; but i don't know how to do so.

推荐答案

您可以将 @Around 方面应用于应修改其返回类型的方法.你可以看看我的博文 关于如何将 Spring AOP facet 添加到 Spring 应用程序,然后编写一个 @Around aspect.我将此方面用于方法的 Memoizing 结果,但在您的情况下,您将获取 ProceedingJoinPoint.proceed() 的返回值,将其类型转换为适当的类,然后修改并返回.

You can apply an @Around aspect to the method whose return type should be modified. You can take a look at my blog post on how to add Spring AOP facet to a Spring application, then write an @Around aspect. I used this aspect for Memoizing results of a method, but in your case, you would take the return value of ProceedingJoinPoint.proceed(), typecast it to appropriate class, then modify it and return it.

如果您打算完全返回一个完全不同的对象,那么这将导致 ClassCastException,除非实际返回的对象是返回类型的子类.

In case you plan to return a completely different object altogether, then that would result in ClassCastException, unless the actual returned object is a subclass of the return type.

这篇关于spring aop如何改变返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆