项目结构图
注意事项:
如果要使用AspectJ完成注解切面需要注意下面的JDK与AspectJ的匹配:
enCoding=?>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">userManager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">class</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">com.tgb.spring.UserManagerImpl</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span>></bean> <bean <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectJAdvice</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">class</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">com.tgb.spring.AspectJAdvice</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span>></bean>
2.UserManager的接口实现
<span >public <span >interface<span > UserManager {
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> addUser(String user<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>,String password); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> delUser(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">abstract</span> String findUserBy<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> modifyUser(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>,String user<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>,String password);
}
3.继承UserManager接口实现UserManagerImpl
<span >public <span >class<span > UserManagerImpl implements UserManager {@Overr<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> addUser(String user<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>,String password) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"> T<a href="https://www.jb51.cc/tag/odo/" target="_blank" >odo</a> <a href="https://m.jb51.cc/tag/auto/" target="_blank" >auto</a>-generated method stub</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----tianjia.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); }@Overr<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> delUser(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"> T<a href="https://www.jb51.cc/tag/odo/" target="_blank" >odo</a> <a href="https://m.jb51.cc/tag/auto/" target="_blank" >auto</a>-generated method stub</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----shanchu.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } @Overr<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> String findUserBy<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----UserManagerImpl.findUserBy<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">if</span>(user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a> <= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">){ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> Ille<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>ArgumentException(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">该<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>不存在</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">jiuqiyu<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ang</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; } @Overr<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> modifyUser(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> user<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>,String password) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"> T<a href="https://www.jb51.cc/tag/odo/" target="_blank" >odo</a> <a href="https://m.jb51.cc/tag/auto/" target="_blank" >auto</a>-generated method stub</span> Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">----xiugai.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); }
}
4.切面实现
import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.pointcut;
@Aspect
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * <a href="https://www.jb51.cc/tag/pointcut/" target="_blank" >pointcut</a> * 定义<a href="https://www.jb51.cc/tag/pointcut/" target="_blank" >pointcut</a>,<a href="https://www.jb51.cc/tag/pointcut/" target="_blank" >pointcut</a>的<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" >名称</a>为aspectjMethod(),此<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" >方法</a>没有返回值和<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a> * 该<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" >方法</a>就是<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>标识,不<a href="https://m.jb51.cc/tag/jinxing/" target="_blank" >进行</a><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a> </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @<a href="https://www.jb51.cc/tag/pointcut/" target="_blank" >pointcut</a>(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">execution(* find*(..))</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> aspectjMethod(){}; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * Before * 在核心业务执行前执行,不能阻止核心业务的<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>。 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m joinPoint </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @Before(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> <a href="https://www.jb51.cc/tag/dob/" target="_blank" >dob</a>efore(JoinPoint joinPoint) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----<a href="https://www.jb51.cc/tag/dob/" target="_blank" >dob</a>efore.invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处意在执行核心业务逻辑前,做一些<a href="https://m.jb51.cc/tag/anquan/" target="_blank" >安全</a>性的判断等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 可通过joinPoint来<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>所需要的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" >内容</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----End of <a href="https://www.jb51.cc/tag/dob/" target="_blank" >dob</a>efore()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * Around * 手动控制<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>核心业务逻辑,<a href="https://m.jb51.cc/tag/yiji/" target="_blank" >以及</a><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>前和<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>后的处理,* * 注意:当核心业务抛异常后,立即<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>,转向Af<a href="https://www.jb51.cc/tag/tera/" target="_blank" >tera</a>dvice * 执行完Af<a href="https://www.jb51.cc/tag/tera/" target="_blank" >tera</a>dvice,再转到ThrowingAdvice * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m pjp * @return * @throws Throwable </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @Around(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> Object doAround(ProceedingJoinPoint pjp) throws Throwable { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----doAround.invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处可以做类似于Before的事情</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>核心逻辑 </span> Object retVal =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> pjp.proceed(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处可以做类似于After的事情</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----End of doAround()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> retVal; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * After * 核心业务逻辑<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>后(<a href="https://www.jb51.cc/tag/baokuo/" target="_blank" >包括</a>正常执行结束和异常<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>),执行此Advice * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m joinPoint </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @After(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> doAfter(JoinPoint joinPoint) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----doAfter.invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处意在执行核心业务逻辑之后,做一些日志记录 *** 作等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 可通过joinPoint来<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>所需要的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" >内容</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----End of doAfter()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * AfterReturning * 核心业务逻辑<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>正常<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>后,不管是否有返回值,正常<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>后,均执行此Advice * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m joinPoint </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @AfterReturning(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span>,returning = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">retVal</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> doReturn(JoinPoint joinPoint,String retVal) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----doReturn().invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">Return Value: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> retVal); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处可以对返回值做进一步处理</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 可通过joinPoint来<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>所需要的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" >内容</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----End of doReturn()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">* * 核心业务逻辑<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>异常<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" >退出</a>后,执行此Advice,处理<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" >错误</a>信息 * * 注意:执行顺序在Around Advice之后 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m joinPoint * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" >ara</a>m ex </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> @AfterThrowing(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span>,throwing = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">ex</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> doThrowing(JoinPoint joinPoint,Exception ex) { Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----doThrowing().invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> <a href="https://www.jb51.cc/tag/cuowu/" target="_blank" >错误</a>信息:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span>+<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">ex.getMessage()); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录 *** 作等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> 可通过joinPoint来<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" >获取</a>所需要的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" >内容</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">-----End of doThrowing()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); }
}
5.ClIEnt端实现
import com.tgb.spring.UserManager;import com.tgb.spring.UserManagerImpl;
<span >public <span >class<span > ClIEnt {
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> main(String[] args) { <a href="https://www.jb51.cc/tag/beanfactory/" target="_blank" >beanfactory</a> factory </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> Cla<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>athXmlApp<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cationContext(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">app<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>cationContext.xml</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); UserManager userManager </span>= (UserManager) factory.getBean(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">userManager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">可以查找张三 </span> userManager.findUserBy<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" >stem</a>.</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">=====我==是==分==割==线=====</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"> 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获 </span> userManager.findUserBy<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> (Ille<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>ArgumentException e) { } }
}
总结
以上是内存溢出为你收集整理的spring注解实现AOP全部内容,希望文章能够帮你解决spring注解实现AOP所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)