如何通过Spring AOP更改返回值 [英] How to change the return value by spring aop

查看:2351
本文介绍了如何通过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方面添加到Spring应用程序,然后编写一个@Around方面.我将此方面用于方法的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天全站免登陆