spring注解实现AOP

spring注解实现AOP,第1张

概述项目结构图注意事项:我原来用的JDK1.7匹配的是aspectjrt.1.6和aspectjweaver.1.6,因此会报错。 如果要使用AspectJ完成注解切面需要注意下面的JDK与AspectJ的匹配:JDK1.6 —— aspectJ1.6JDK1.7 —— aspectJ1.7.3+1,Bean的配文件。 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd" ><aop:aspectj-autoproxy></aop:aspectj-autoproxy><bean id="userManager" class="com.tgb.spring.UserManagerImpl"></bean><bean id="aspectJAdvice" class="com.tgb.spring.AspectJAdvice"></bean></beans> 2.UserManager的接口实现  package com.tgb.spring;public interface UserManager {public abstract void addUser(String userName,String password);public abstract void delUser(int userId);public abstract String findUserById(int userId);public abstract void modifyUser(int userId,String userName,String password);} 3.继承UserManager接口实现UserManagerImplpackage com.tgb.spring;public class UserManagerImpl implements UserManager {@Overridepublic void addUser(String userName, String password) {// TODO Auto-generated method stubSystem.out.println("----tianjia.delUser()----");}@Overridepublic void delUser(int userId) {// TODO Auto-generated method stubSystem.out.println("----shanchu.delUser()----");}@Overridepublic String findUserById(int userId) {System.out.println("----UserManagerImpl.findUserById()----");if(userId <= 0){throw new IllegalArgumentException("该用户不存在");}return "jiuqiyuliang";}@Overridepublic void modifyUser(int userId, String userName, String password) {// TODO Auto-generated method stubSystem.out.println("----xiugai.delUser()----");}}4.切面实现package com.tgb.spring;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;@Aspectpublic class AspectJAdvice {/*** Pointcut* 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数* 该方法就是一个标识,不进行调用*/@Pointcut("execution(* find*(..))")private void aspectjMethod(){};/*** Before* 在核心业务执行前执行,不能阻止核心业务的调用。* @param joinPoint*/@Before("aspectjMethod()")public void doBefore(JoinPoint joinPoint) {System.out.println("-----doBefore.invoke-----");System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");System.out.println(" 可通过joinPoint来获取所需要的内容");System.out.println("-----End of doBefore()------");}/*** Around* 手动控制调用核心业务逻辑,以及调用前和调用后的处理,** 注意:当核心业务抛异常后,立即退出,转向AfterAdvice* 执行完AfterAdvice,再转到ThrowingAdvice* @param pjp* @return* @throws Throwable*/@Around(value = "aspectjMethod()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println("-----doAround.invoke-----");System.out.println(" 此处可以做类似于Before的事情");//调用核心逻辑Object retVal = pjp.proceed();System.out.println(" 此处可以做类似于After的事情");System.out.println("-----End of doAround()------");return retVal;}/*** After* 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice* @param joinPoint*/@After(value = "aspectjMethod()")public void doAfter(JoinPoint joinPoint) {System.out.println("-----doAfter.invoke-----");System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录 *** 作等等");System.out.println(" 可通过joinPoint来获取所需要的内容");System.out.println("-----End of doAfter()------");}/*** AfterReturning* 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice* @param joinPoint*/@AfterReturning(value = "aspectjMethod()", returning = "retVal")public void doReturn(JoinPoint joinPoint, String retVal) {System.out.println("-----doReturn().invoke-----");System.out.println("Return Value: " + retVal);System.out.println(" 此处可以对返回值做进一步处理");System.out.println(" 可通过joinPoint来获取所需要的内容");System.out.println("-----End of doReturn()------");}/*** 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息** 注意:执行顺序在Around Advice之后* @param joinPoint* @param ex*/@AfterThrowing(value = "aspectjMethod()", throwing = "ex")public void doThrowing(JoinPoint joinPoint, Exception ex) {System.out.println("-----doThrowing().invoke-----");System.out.println(" 错误信息:"+ex.getMessage());System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录 *** 作等等");System.out.println(" 可通过joinPoint来获取所需要的内容");System.out.println("-----End of doThrowing()------");}}5.Client端实现package com.tgb.spring.client;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;impo

项目结构图

注意事项:

 

  如果要使用AspectJ完成注解切面需要注意下面的JDK与AspectJ的匹配:

 

enCoding=?> ==== http: http: http:
<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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;userManager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;class</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;com.tgb.spring.UserManagerImpl</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectJAdvice</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;class</span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;com.tgb.spring.AspectJAdvice</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span>></bean>  

 

2.UserManager的接口实现

 

 

<span >public
<span >interface
<span > UserManager {

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt; 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"&gt;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"&gt;abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;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"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;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"&gt;abstract</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;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"&gt;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"&gt; 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"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----tianjia.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);         }@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"&gt;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"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----shanchu.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);  } @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"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;){          </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;throw</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;该<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>不存在</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      }      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;;  } @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"&gt;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"&gt;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"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;----xiugai.delUser()----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);  }    

}

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 >public <span >class<span > AspectJAdvice {

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*  * <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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;  @<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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;execution(* find*(..))</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)  </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt; aspectjMethod(){};  </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*   * 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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;    @Before(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;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"&gt; <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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----<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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处意在执行核心业务逻辑前,做一些<a href="https://m.jb51.cc/tag/anquan/" target="_blank" >安全</a>性的判断等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 可通过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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);  }  </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*   * 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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;   @Around(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----doAround.invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处可以做类似于Before的事情</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;<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"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处可以做类似于After的事情</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----End of doAround()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; retVal;  }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*   * 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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;  @After(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;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"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----doAfter.invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处意在执行核心业务逻辑之后,做一些日志记录 *** 作等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 可通过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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----End of doAfter()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);  }    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*   * 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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;   @AfterReturning(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span>,returning = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;retVal</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;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"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----doReturn().invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;Return Value: </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span> +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处可以对返回值做进一步处理</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 可通过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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----End of doReturn()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);  }  </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;/*</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;*  * 核心业务逻辑<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"&gt;*/</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;  @AfterThrowing(value </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;aspectjMethod()</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span>,throwing = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;ex</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;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"&gt; 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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----doThrowing().invoke-----</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; <a href="https://www.jb51.cc/tag/cuowu/" target="_blank" >错误</a>信息:</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span>+<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录 *** 作等等</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt; 可通过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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;-----End of doThrowing()------</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);    }    

}

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"&gt;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"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt; 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"&gt;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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      UserManager userManager </span>= (UserManager) factory.getBean(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;userManager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;可以查找张三  </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"&gt;1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      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"&gt;out</span>.println(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;=====我==是==分==割==线=====</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;try</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; {          </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000"&gt; 查不到数据,会抛异常,异常会被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"&gt;0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;);      } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;catch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; (Ille<a href="https://www.jb51.cc/tag/gal/" target="_blank" >gal</a>ArgumentException e) {      }  }  

}

 

总结

以上是内存溢出为你收集整理的spring注解实现AOP全部内容,希望文章能够帮你解决spring注解实现AOP所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存