由于单独的Aop需要在Xml中配置好多,在Spring中,Spring提供了接口来拦截方法,这使得更简便
下面是例子:
beans.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-lazy-init="false" default-autowire="no"> <bean id="around" class="org.hzy.advices.AroundAdvice"></bean> <bean id="before" class="org.hzy.advices.BeforeAdvice"></bean> <bean id="after" class="org.hzy.advices.AfterAdvice"></bean> <bean id="student" class="org.hzy.dao.impl.StudentImpl"></bean> <aop:config> <aop:pointcut id="target" expression="execution(* org.hzy.dao.impl.StudentImpl.addStudent(..))"/> <!-- <aop:advisor id="be" advice-ref="before" pointcut-ref="target"/> --> <!-- <aop:advisor id="af" advice-ref="after" pointcut-ref="target"/> --> <aop:advisor id="ar" advice-ref="around" pointcut-ref="target"/><!-- 不是不需要切面,而是它已经包含了切面 --> </aop:config> </beans>
package org.hzy.advices; import java.lang.reflect.Method; import org.aopalliance.intercept.Invocation; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class AroundAdvice implements MethodInterceptor{//相当于环绕通知 public Object invoke(MethodInvocation arg0) throws Throwable {//arg0相当于struts2的拦截器的调用者actionInvocation // TODO Auto-generated method stub System.out.println("进入拦截方法!!!!!!!!"); Method me=arg0.getMethod(); System.out.println(me.getName()); System.out.println(arg0.getArguments()[0]); Object result=null; String stu_name=arg0.getArguments()[0].toString(); if("hzy".equals(stu_name)){ result= arg0.proceed(); }else{ System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入."); } return result; } }
package org.hzy.advices; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class BeforeAdvice implements MethodBeforeAdvice{//相当于前置通知 public void before(Method method, Object[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println( " 这是BeforeAdvice类的before方法. " ); // args[0]="hzy"; System.out.println(args[0]); } }
package org.hzy.advices; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class AfterAdvice implements AfterReturningAdvice{//相当于后置通知 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("这是AfterAdvice类的afterReturning方法."); System.out.println(target.getClass()); } }
package org.hzy.dao; public interface IStudent { public void addStudent(String name); }
package org.hzy.dao.impl; import org.hzy.dao.IStudent; public class StudentImpl implements IStudent { public void addStudent(String name) { // TODO Auto-generated method stub System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " ); } }
package org.hzy.test; import org.hzy.dao.IStudent; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest2 { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans1.xml"); IStudent st=(IStudent) ctx.getBean("student"); st.addStudent("aa"); } }