1. 程式人生 > >第七講:7.2 spring AOP後置-環繞

第七講:7.2 spring AOP後置-環繞

一,後置 1,studentServiceAspect類新增doAfter方法,public void doAfter(JoinPoint jp){        System.out.println("類名:"+jp.getTarget().getClass().getName());        System.out.println("方法名:"+jp.getSignature().getName());        System.out.println("引數:"+jp.getArgs()[0]);        System.out.println("開始新增後置通知:……");2,beans.xml新增after標籤

<?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.xsd
        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean> <bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean> <aop:config>     <aop:aspect id="suibiandingyiaop"
 ref="studentServiceAspect">        <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>        <aop:before method="doBefor" pointcut-ref="bussnessService"/>        <aop:after method="doAfter" pointcut-ref="bussnessService"/>     </aop:aspect> </aop:config> </beans>3,測試-執行(程式碼同上) 二,環繞 1,studentServiceAspect類新增doAround方法,獲取返回值,Object object = pjp.proceed();的作用就是呼叫了一下方法,返回值就是方法的返回值。public Object doAround(ProceedingJoinPoint pjp) throws Throwable{        System.out.println("新增學生前……");        Object object = pjp.proceed();        System.out.println("新增學生後……");        return object;     }2,beans.xml新增around標籤 <?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.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean> <bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean> <aop:config>     <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">        <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>        <aop:before method="doBefor" pointcut-ref="bussnessService"/>        <aop:after method="doAfter" pointcut-ref="bussnessService"/>        <aop:around method="doAround" pointcut-ref="bussnessService"/>     </aop:aspect> </aop:config> </beans>3,測試-執行(程式碼同上) 三,異常通知 1,studentServiceAspect類新增doAfterThrowing方法,public void doAfterThrowing(JoinPoint pjp,Throwable ex) throws Throwable{        System.out.println("異常通知:");        System.out.println("異常資訊:"+ex.getMessage());     }2,beans.xml新增throwing標籤 <?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.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect"></bean> <bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl"></bean> <aop:config>     <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">        <aop:pointcut expression="execution(* com.cruise.service.*.*(..))"id="bussnessService"/>        <aop:before method="doBefor" pointcut-ref="bussnessService"/>        <aop:after method="doAfter" pointcut-ref="bussnessService"/>        <aop:around method="doAround" pointcut-ref="bussnessService"/>        <aop:after-throwing method="doAfterThrowing" pointcut-ref="bussnessService"throwing="ex"/>     </aop:aspect> </aop:config> </beans>3,StudentServiceImpl 新增測試資料System.out.println(1/0)package com.cruise.service.impl;import com.cruise.service.StudentService;public class StudentServiceImpl implements StudentService{     @Override     public void addStudent(String name) {        System.out.println("新增學生:"+name);        System.out.println(1/0);     } }4,測試-執行(同上)