四、通知的创建 通知有:前置通知、后置通知、环绕通知和异常通知; 下面我们分别来看一下: 1、创建PersonService接口: package cn.csdn.service; import java.util.Date; //抽象主题角色 public interface PersonService { public void Study(String name,Date date); } 2、 创建实现PersonService接口的类: package cn.csdn.service; import java.util.Date; //主角 public class PersonServiceBean implements PersonService{ @Override public void Study(String name, Date date) { System.out.println("学习的科目是"+name+"学习的时间是"+date); } } 3、 配置bean.Xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=http://www.springframework.org/schema/beans xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <!-- 环绕通知 --> <bean id="myAroundAdvice" class="cn.csdn.advice.MyAroundAdvice"/> <!-- 配置前置通知 --> <bean id="myBeforeAdvice" class="cn.csdn.advice.MyBeforeAdvice"/> <!-- 配置后置通知 --> <bean id="myAfterAdvice" class="cn.csdn.advice.MyAfterAdvice"/> <!-- 配置异常通知 --> <bean id="myThrowsAdvice" class="cn.csdn.advice.MyThrowsAdvice"/> <!-- 引入通知 --> <bean id="auditableBean" class="cn.csdn.advice.AuditableBean"/> <!-- 业务bean --> <bean id="personServiceBean" class="cn.csdn.service.PersonServiceBean"/> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--代理接口--> <property name="proxyInterfaces"> <list> <value>cn.csdn.service.PersonService</value> <value>cn.csdn.service.Auditable</value> </list> </property> <!--通知名称--> <property name="interceptorNames"> <list> <value>myAroundAdvice</value> <value>myBeforeAdvice</value> <value>myAfterAdvice</value> <value>auditableBean</value> </list> </property> <!--通知对象--> <property name="target"> <ref bean="personServiceBean"/> </property> </bean> </beans> 4、 前置通知的实现: package cn.csdn.advice; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MyBeforeAdvice implements MethodBeforeAdvice{ @Override public void before(Method method, Object[] arg1, Object arg2)throws Throwable { //第一个参数方法对象 // 方法参数 //目标对象 Object obj =null; if(arg1.length>0){ obj= arg1[0]; System.out.println("before-----------方法的名称:"+method.getName()+" 方法的第一个参数值"+obj.toString()+" 目标对象是:"+arg2.getClass()); } } } 5、 后置通知的实现: package cn.csdn.advice; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class MyAfterAdvice implements AfterReturningAdvice{ @Override public void afterReturning(Object returnValue, Method arg1, Object[] arg2,Object arg3) throws Throwable { //执行目标方法之后执行此操作 System.out.println("after-------方法的返回值是:"); } } 6、 环绕通知的实现: package cn.csdn.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyAroundAdvice implements MethodInterceptor{ @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("执行前"); Object object=methodInvocation.proceed(); System.out.println("执行后"); //ProxyFactoryBean return object; } } 7、 异常通知的实现: package cn.csdn.advice; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; public class MyThrowsAdvice implements ThrowsAdvice{ //异常处理操作 public void afterThrowing(Method method,Object[] args, Object target,Throwable throwable){ System.out.println("exception------"+throwable.getMessage()); } } 8、 引入通知的实现: a) 创建接口Auditable: package cn.csdn.advice; import java.util.Date; public interface Auditable { //添加一个属性 记录最后一次修改的时间的操作 void setLastModifiedDate(Date laseModidiedDate); Date getLastModifiedDate(); } b) 创建实现Auditable接口的类: package cn.csdn.advice; import java.util.Date; import org.springframework.aop.support.DelegatingIntroductionInterceptor; public class AuditableBean extends DelegatingIntroductionInterceptor implements Auditable{ private Date laseModidiedDate; @Override public void setLastModifiedDate(Date laseModidiedDate) { this.laseModidiedDate=laseModidiedDate; } @Override public Date getLastModifiedDate() { return laseModidiedDate; } } 9、 测试类(AppMain): package cn.csdn.junit; import java.util.Date; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.csdn.advice.Auditable; import cn.csdn.service.PersonService; public class AppMain { @Test public void test() { ApplicationContext context=new ClassPathXmlApplicationContext("classpath:bean.xml"); PersonService pService=(PersonService) context.getBean("proxyFactoryBean"); pService.Study("java",new Date()); Auditable au = (Auditable)pService; au.setLastModifiedDate(new Date()); System.out.println(au.getLastModifiedDate()); } } 10、 测试结果: 执行前 before-----------方法的名称:Study 方法的第一个参数值java 目标对象是:class cn.csdn.service.PersonServiceBean 学习的科目是java学习的时间是Wed Apr 18 21:03:55 CST 2012 after-------方法的返回值是:null 执行后 执行前 before-----------方法的名称:setLastModifiedDate 方法的第一个参数值Wed Apr 18 21:03:55 CST 2012 目标对象是:class cn.csdn.service.PersonServiceBean after-------方法的返回值是:null 执行后 执行前 after-------方法的返回值是:Wed Apr 18 21:03:55 CST 2012 执行后 Wed Apr 18 21:03:55 CST 2012
转载于:https://www.cnblogs.com/springside4/archive/2012/04/18/2481059.html