spring在事务中定义通知
?
切面(Aspect):需要实现的交叉功能
引入(Introduction):允许为已存在类添加新方法和属性。
目标对象(Target):被通知的对象。既可是编写的类也可是添加定制行为的第三方类。
代理(Proxy):将通知应用到目标对象后创建的对象。
在指定接入点被织入到目标对象中,注入发生在目标对象生命周期的多个点上:
编译器:切面子啊目标对象编译时织入(需特殊的编译器)
运行期:切面在用系统运行时织入(通常,AOP容器将在织入切面时动态)
?
?
?
通知类型
接口
描述
Around
import org.aopalliance.intercept.MethodInterceptor;
拦截目标方法的前后调用
Before
import org.springframework.aop.MethodBeforeAdvice;
拦截目标方法前调用
After
import org.springframework.aop.AfterReturningAdvice;
拦截目标方法的之后调用
Throws
import org.springframework.aop.ThrowsAdvice;
目标抛出异常时调用
?
?
?
?
前置通知需要实现MethodBeforeAdvice接口
?
?
?
package com.csdn.util;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class StudentAdvice implements MethodBeforeAdvice {@Overridepublic void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println("开始学习....的前置通知!!");}}?
?
注:Method 的一个参数是要实现通知的目标方法(连接点), arg1 为参数,arg2为目标对象。
?
目标对象
?
package com.csdn.service;public class StudentServiceImpl implements StudentService {@Overridepublic void study() {System.out.println("调用学习的方法...");}@Overridepublic void paly() {System.out.println("调用paly的方法....");}}?
?
?
Student.xml中如下
?
?
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 前置通知 --><bean id="studentAdvice" /><!-- 目标对象 --><bean id="studentServiceImpl" ref="studentServiceImpl"></property></bean></beans>?
注:测试接口时调用studentProxyFactoryBean如下
?ApplicationContext?ac?=?ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");StudentService ss = (StudentService) ac.getBean("studentProxyFactoryBean");
?
后置通知需要实现AfterReturningAdvice接口如下
?
package com.csdn.util;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class StudentAfterAdvice implements AfterReturningAdvice {@Overridepublic void afterReturning(Object returnType, Method method, Object[] arg2,Object targeType) throws Throwable {System.out.println("后置处理 ");}}目标对象package com.csdn.service;public class StudentServiceImpl implements StudentService {@Overridepublic void study() {System.out.println("调用学习的方法...");}@Overridepublic void paly() {System.out.println("调用paly的方法....");}}?
?
?
?
?
?
3.环绕通知环绕通知实现MethodInterceptor接口:实现的方法为package com.csdn.util;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class StudentAround implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("前");Object obj=invocation.proceed();//调用目标方法System.out.println("后");return obj;}}与前面的两种区别?MethodInterceptor能控制方法是否真的被调用。通过调用MethodInvocation的Proceed()方法来调用目标方法。?Method能控制返回的对象。4.异常通知异常通知需要是想的接口是ThrowsAdvicepackage com.csdn.util;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class StudentException implements ThrowsAdvice {public void afterThrowing(Method method, Object[] args, Object target,Exception ex) {System.out.println("方法的名称:" + method + "方法的参数" + args.length + "目标对象:"+ target.getClass().getName() + "异常" + ex.getMessage());}}Xml中如下<!-- 异常对象的获取 --><bean id="studentException" /><!-- 目标对象 --><bean id="studentServiceImpl" ref="studentServiceImpl"></property></bean>?
Student.xml中如下<!-- 后置通知 --><bean id="studentAfterAdvice"ref="studentServiceImpl"></property></bean>注:测试接口时调用studentProxyFactoryBean如下ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");StudentService ss = (StudentService) ac.getBean("studentProxyFactoryBean");?