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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:45:24  来源:igfitidea点击:

How to change the return value by spring aop

javaspringaop

提问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 @Aroundaspect 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 @Aroundaspect. I used this aspect for Memoizingresults 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,除非实际返回的对象是返回类型的子类。