java spring aop如何改变返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14017860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the return value by spring aop
提问by cleverUtd
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.
我在DAO层有一个返回值的方法,我想通过spring AOP改变返回值,根据不同的需求,然后发送到SERVICE层的相应方法;但我不知道该怎么做。
回答by Vikdor
You can apply an @Around
aspect to the method whose return type should be modified. You can take a look at my blog poston 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.
您可以将@Around
方面应用于应修改其返回类型的方法。您可以查看我关于如何将 Spring AOP facet 添加到 Spring 应用程序的博客文章,然后编写一个@Around
切面。我将此方面用于Memoizing
方法的结果,但在您的情况下,您将获取 的返回值ProceedingJoinPoint.proceed()
,将其类型转换为适当的类,然后对其进行修改并返回。
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.
如果您计划完全返回一个完全不同的对象,那么这将导致 ClassCastException,除非实际返回的对象是返回类型的子类。