spring28: aspectJ--基于xml
生活随笔
收集整理的這篇文章主要介紹了
spring28: aspectJ--基于xml
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AspectJ是一個基于java的aop框架。
切入點表達式: ?execution() 用于描述方法
? ? ? ? ? ? ? ?語法: ?execution(修飾符 ?返回值 包.類.方法名(參數) throws 異常)
?
切面類:
package com.atchina.d_spring_aop_xml;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint;public class MyAspect {public void before(JoinPoint joinPoint){System.out.println("aspect xml 事務開啟 before "+joinPoint.getSignature().getName());}public void after(JoinPoint joinPoint, Object ret){System.out.println("aspect xml 事務開啟 after "+joinPoint.getSignature().getName()+", -->"+ret);}public Object around(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("環繞通知,在目標方法之前...增強功能");// 手動執行目標方法Object obj = joinPoint.proceed();if(null !=obj){// 修改目標方法的執行結果obj = "update";}System.out.println("環繞通知,在目標方法之后...增強功能");return obj;}public void myThrowing(JoinPoint joinPoint, Throwable e){System.out.println("aspect xml 異常通知. "+joinPoint.getSignature().getName()+", 異常通知信息"+e.getMessage());}public void myAfter(JoinPoint joinPoint){System.out.println("aspect xml 最終通知 "+joinPoint.getSignature().getName());} }?接口
public interface UserService {void addUser();String updateUser(); } public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("config adduser...");}@Overridepublic String updateUser() {System.out.println("config updateUser...");// int i = 1/0; // 測試異常通知return "updateUserRet";} }配置文件
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1.創建目標類 --><bean id="userService" class="com.atchina.d_spring_aop_xml.UserServiceImpl"/><!-- 2.創建切面類 --><bean id="myAspectId" class="com.atchina.d_spring_aop_xml.MyAspect"/><!-- 3. aop編程使用<aop:config>配置<aop:aspect> 將切面類聲明"切面",從而獲得通知方法ref:切面類引用<aop:pointcut> 聲明一個切入點,所有的通知都可以使用expression:切入點表達式id:名稱, 用于其他通知引用--> <aop:config><aop:aspect ref="myAspectId"><aop:pointcut expression="execution(* com.atchina.d_spring_aop_xml.UserServiceImpl.*(..))" id="myPointCut" /><!-- 前置通知 method: 通知,及方法名pointcut: 切入點表達式,此表達式只能在當前通知中使用。pointcut-ref: 切入點引用,可以與其他通知共享切入點通知方法格式:public void before(JoinPoint joinPoint)參數1:org.aspectj.lang.JoinPoint 用于描述連接點(目標方法),獲得目標方法名等<aop:before method="before" pointcut-ref="myPointCut"/> --><!-- 后置通知 method: 通知,及方法名pointcut: 切入點表達式,此表達式只能在當前通知中使用。pointcut-ref: 切入點引用,可以與其他通知共享切入點returning: 通知方法第二個參數的名稱通知方法格式:public void after(JoinPoint joinPoint, Object ret)參數1:org.aspectj.lang.JoinPoint 用于描述連接點(目標方法),獲得目標方法名等參數2:類型Object, 參數名returning="ret"配置的 如下:<aop:after-returning method="after" pointcut-ref="myPointCut" returning="ret"/>--><!-- 環繞通知 method: 通知,及方法名pointcut: 切入點表達式,此表達式只能在當前通知中使用。pointcut-ref: 切入點引用,可以與其他通知共享切入點通知方法格式:public Object around(ProceedingJoinPoint joinPoint) throws Throwable返回值類型: Object方法名: 任意參數: org.aspectj.lang.ProceedingJoinPoint 拋出異常:執行目標方法: Object obj = joinPoint.proceed(); 如下:<aop:around method="around" pointcut-ref="myPointCut"/>--><!-- 異常通知 method: 通知,及方法名pointcut: 切入點表達式,此表達式只能在當前通知中使用。pointcut-ref: 切入點引用,可以與其他通知共享切入點throwing: 通知方法第二個參數的名稱通知方法格式:public void myThrowing(JoinPoint joinPoint, Throwable e)參數1:org.aspectj.lang.JoinPoint 用于描述連接點(目標方法),獲得目標方法名等參數2:類型throwing, 參數名throwing="e"配置的 如下:<aop:after-throwing method="myThrowing" pointcut-ref="myPointCut" throwing="e"/>--><!-- 最終通知 --><aop:after method="myAfter" pointcut-ref="myPointCut" /></aop:aspect></aop:config> </beans>?測試類
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactoryBean {@Testpublic void test(){String xmlPath = "com/atchina/d_spring_aop_xml/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService)ac.getBean("userService");userService.addUser();String ret = userService.updateUser();System.out.println(ret);} }?
總結
以上是生活随笔為你收集整理的spring28: aspectJ--基于xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring18-4: spring a
- 下一篇: 《编码:隐匿在计算机软硬件背后的语言(美