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jnlzKGb64nXnhyM7Sqe8bLnXBF5heM9JMmt8XjdeHehvKWMD0CbkVOjwWWaRd+fEtdz86lh7Yg2PrGaYMZDzhVID+q/TjDj7QM5VwCRH1Q60hqPIOeqOIznmAjJbTwA0IDxgFqA8YBagPGAWoDxgFrIbDzkXAEamY2HnCtAI7PxkHNVF/7/hczGQ841CbfrmirxVgPBjFcg55rB97spN/FAvNXAZHwwGAyzCIVCjjBegZxr7o33QLwVw2R8OBx+4IEH5syZM2fOnHnz5i1atMjtdldXV4fDYacYL3XOFesnbE5IEf2PsYrQm1AHA/FW7Hwxr/Hl5eVPPfXUunXr6urqmpqanHWNR5LnXE2RIsLzRLdzqwYGrzTF462JQtx0O16X/sUXX9y3b19nZ6euu1Pa8fLnXE2BI5JEQRbVg3grYyF+ldeNHx0dnZiYMG5SQ6FQOBw+ePCgobuzjJc552oynvvsx6J6pnVITDeEysRbjZm68WNjY3fu3AlgTE5ORiKRyclJY04wGAyFQsU3XvacK88kxB261d49nOLxVpT4j9ONn5iY0J22IBQKOcJ46XOu8bWoXCjxKJCcRWzCPyiV462uxCi2uvF+v5/227AcR5jn8ZBzVRr+/5huPC335OSk/q8JYb5zhZyryuAfVCZ042mzeQhjPIKcq5pw4q0GuvFe27j0bQBAUPxp4kII9dqmJ0F/gjNnztTV1W3dunXr1q3bMOg5hWQrSV1d3ZkzZ/oB0RiwQSbG2ynXxHCCurq6oaEh/S7BsQwMDNTV1Q0DonGLZHR01Ov1+nw+/cnM5OSk1+vNxPgR2xj7NlpF27dvD4fDodBkKBQKBpNTIBDUH/kXZdL3HggE/f7AxMTExMSdt956y35TDygWYyzGE/h8Pv2LVf3JTGbG/38JQEbLC+H8RgAAAABJRU5ErkJggg==" alt="">
注意事项:
如果要使用AspectJ完成注解切面需要注意下面的JDK与AspectJ的匹配:
文件。
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id=<span style="color: #800000">"</span><span style="color: #800000">userManager</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">com.tgb.spring.UserManagerImpl</span><span style="color: #800000">"</span>></bean>
<bean id=<span style="color: #800000">"</span><span style="color: #800000">aspectJAdvice</span><span style="color: #800000">"</span> <span style="color: #0000ff">class</span>=<span style="color: #800000">"</span><span style="color: #800000">com.tgb.spring.AspectJAdvice</span><span style="color: #800000">"</span>></bean>
2.UserManager的接口实现
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">void</span><span style="color: #000000"> addUser(String userName,String password);
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">void</span> delUser(<span style="color: #0000ff">int</span><span style="color: #000000"> userId);
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> String findUserById(<span style="color: #0000ff">int</span><span style="color: #000000"> userId);
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">void</span> modifyUser(<span style="color: #0000ff">int</span><span style="color: #000000"> userId,String userName,String password);
}
3.继承UserManager接口实现UserManagerImpl
@Override
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> addUser(String userName,String password) {
</span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span>
System.<span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">----tianjia.delUser()----</span><span style="color: #800000">"</span><span style="color: #000000">);
}
@Override
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> delUser(<span style="color: #0000ff">int</span><span style="color: #000000"> userId) {
</span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span>
System.<span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">----shanchu.delUser()----</span><span style="color: #800000">"</span><span style="color: #000000">);
}
@Override
</span><span style="color: #0000ff">public</span> String findUserById(<span style="color: #0000ff">int</span><span style="color: #000000"> userId) {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">----UserManagerImpl.findUserById()----</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(userId <= <span style="color: #800080">0</span><span style="color: #000000">){
</span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> IllegalArgumentException(<span style="color: #800000">"</span><span style="color: #800000">该<a href="/tag/yonghu/" target="_blank" class="keywords">用户</a>不存在</span><span style="color: #800000">"</span><span style="color: #000000">);
}
</span><span style="color: #0000ff">return</span> <span style="color: #800000">"</span><span style="color: #800000">jiuqiyuliang</span><span style="color: #800000">"</span><span style="color: #000000">;
}
@Override
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> modifyUser(<span style="color: #0000ff">int</span><span style="color: #000000"> userId,String password) {
</span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span>
System.<span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">----xiugai.delUser()----</span><span style="color: #800000">"</span><span style="color: #000000">);
}
}
4.切面实现
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 style="color: #008000">/*</span><span style="color: #008000">*
* Pointcut
* 定义Pointcut,Pointcut的<a href="/tag/mingcheng/" target="_blank" class="keywords">名称</a>为aspectjMethod(),此<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>没有返回值和参数
* 该<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>就是一个标识,不进行<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>
</span><span style="color: #008000">*/</span><span style="color: #000000">
@Pointcut(</span><span style="color: #800000">"</span><span style="color: #800000">execution(* find*(..))</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span><span style="color: #000000"> aspectjMethod(){};
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* Before
* 在核心业务执行前执行,不能阻止核心业务的<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>。
* @param joinPoint
</span><span style="color: #008000">*/</span><span style="color: #000000">
@Before(</span><span style="color: #800000">"</span><span style="color: #800000">aspectjMethod()</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doBefore(JoinPoint joinPoint) {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----doBefore.invoke-----</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处意在执行核心业务逻辑前,做一些安全性的判断等等</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 可通过joinPoint来<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>所需要的<a href="/tag/neirong/" target="_blank" class="keywords">内容</a></span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----End of doBefore()------</span><span style="color: #800000">"</span><span style="color: #000000">);
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* Around
* 手动控制<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>核心业务逻辑,以及<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>前和<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>后的处理,*
* 注意:当核心业务抛异常后,立即<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>,转向AfterAdvice
* 执行完AfterAdvice,再转到ThrowingAdvice
* @param pjp
* @return
* @throws Throwable
</span><span style="color: #008000">*/</span><span style="color: #000000">
@Around(value </span>= <span style="color: #800000">"</span><span style="color: #800000">aspectjMethod()</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">public</span><span style="color: #000000"> Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----doAround.invoke-----</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处可以做类似于Before的事情</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>核心逻辑 </span>
Object retVal =<span style="color: #000000"> pjp.proceed();
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处可以做类似于After的事情</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----End of doAround()------</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span><span style="color: #000000"> retVal;
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* After
* 核心业务逻辑<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>后(<a href="/tag/baokuo/" target="_blank" class="keywords">包括</a>正常执行结束和异常<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>),执行此Advice
* @param joinPoint
</span><span style="color: #008000">*/</span><span style="color: #000000">
@After(value </span>= <span style="color: #800000">"</span><span style="color: #800000">aspectjMethod()</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doAfter(JoinPoint joinPoint) {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----doAfter.invoke-----</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处意在执行核心业务逻辑之后,做一些日志记录操作等等</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 可通过joinPoint来<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>所需要的<a href="/tag/neirong/" target="_blank" class="keywords">内容</a></span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----End of doAfter()------</span><span style="color: #800000">"</span><span style="color: #000000">);
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* AfterReturning
* 核心业务逻辑<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>正常<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>后,不管是否有返回值,正常<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>后,均执行此Advice
* @param joinPoint
</span><span style="color: #008000">*/</span><span style="color: #000000">
@AfterReturning(value </span>= <span style="color: #800000">"</span><span style="color: #800000">aspectjMethod()</span><span style="color: #800000">"</span>,returning = <span style="color: #800000">"</span><span style="color: #800000">retVal</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doReturn(JoinPoint joinPoint,String retVal) {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----doReturn().invoke-----</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">Return Value: </span><span style="color: #800000">"</span> +<span style="color: #000000"> retVal);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处可以对返回值做进一步处理</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 可通过joinPoint来<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>所需要的<a href="/tag/neirong/" target="_blank" class="keywords">内容</a></span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----End of doReturn()------</span><span style="color: #800000">"</span><span style="color: #000000">);
}
</span><span style="color: #008000">/*</span><span style="color: #008000">*
* 核心业务逻辑<a href="/tag/diaoyong/" target="_blank" class="keywords">调用</a>异常<a href="/tag/tuichu/" target="_blank" class="keywords">退出</a>后,执行此Advice,处理<a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>信息
*
* 注意:执行顺序在Around Advice之后
* @param joinPoint
* @param ex
</span><span style="color: #008000">*/</span><span style="color: #000000">
@AfterThrowing(value </span>= <span style="color: #800000">"</span><span style="color: #800000">aspectjMethod()</span><span style="color: #800000">"</span>,throwing = <span style="color: #800000">"</span><span style="color: #800000">ex</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> doThrowing(JoinPoint joinPoint,Exception ex) {
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----doThrowing().invoke-----</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> <a href="/tag/cuowu/" target="_blank" class="keywords">错误</a>信息:</span><span style="color: #800000">"</span>+<span style="color: #000000">ex.getMessage());
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等</span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000"> 可通过joinPoint来<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>所需要的<a href="/tag/neirong/" target="_blank" class="keywords">内容</a></span><span style="color: #800000">"</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">-----End of doThrowing()------</span><span style="color: #800000">"</span><span style="color: #000000">);
}
}
5.Client端实现
import com.tgb.spring.UserManager;
import com.tgb.spring.UserManagerImpl;
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) {
<a href="/tag/beanfactory/" target="_blank" class="keywords">beanfactory</a> factory </span>= <span style="color: #0000ff">new</span> ClassPathXmlApplicationContext(<span style="color: #800000">"</span><span style="color: #800000">applicationContext.xml</span><span style="color: #800000">"</span><span style="color: #000000">);
UserManager userManager </span>= (UserManager) factory.getBean(<span style="color: #800000">"</span><span style="color: #800000">userManager</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">可以查找张三 </span>
userManager.findUserById(<span style="color: #800080">1</span><span style="color: #000000">);
System.</span><span style="color: #0000ff">out</span>.println(<span style="color: #800000">"</span><span style="color: #800000">=====我==是==分==割==线=====</span><span style="color: #800000">"</span><span style="color: #000000">);
</span><span style="color: #0000ff">try</span><span style="color: #000000"> {
</span><span style="color: #008000">//</span><span style="color: #008000"> 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获 </span>
userManager.findUserById(<span style="color: #800080">0</span><span style="color: #000000">);
} </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (IllegalArgumentException e) {
}
}
}