代理的三种配置
beans配置文件
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="IStudentService" class="com.my.servic.impl.StudentServiceImpl"></bean> <bean id="IEmpService" class="com.my.servic.impl.EmpServiceImpl"></bean> <bean id="logAdivce" class="com.my.advice.LogAdivce"></bean> <bean id="customerAdvice" class="com.my.advice.CustomerAdvice"></bean> <bean id="annotationAdvice" class="com.my.advice.AnnotationAdvice"></bean> <aop:config> <aop:pointcut expression="execution(* com.my.servic.impl.*.*(..))" id="pointcut" /> <aop:advisor advice-ref="logAdivce" pointcut-ref="pointcut" /> </aop:config> <!-- 自定义配置通知 --> <aop:config> <aop:aspect ref="customerAdvice"> <aop:pointcut expression="execution(* com.my.servic.impl.*.add(..))" id="pointcut1" /> <aop:pointcut expression="execution(* com.my.servic.impl.*.delete(..))" id="pointcut2" /> <aop:around method="roundMethod" pointcut-ref="pointcut1" /> <aop:before method="methodBefore" pointcut-ref="pointcut1" /> <aop:after method="methodAfter" pointcut-ref="pointcut2" /> </aop:aspect> </aop:config> <!-- 扫描注解 组件 --> <context:component-scan base-package="*"></context:component-scan> <!-- 注解方式 --> <aop:aspectj-autoproxy /> <!-- 使用ProxyFactoryBean --> <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <list> <value>com.my.servic.IStudentService</value> <value>com.my.servic.IEmpService</value> </list> </property> <property name="target"> <ref local="IStudentService" /> </property> <property name="interceptorNames"> <list> <value>logAdivce</value> </list> </property> </bean> </beans>
注解写法
package com.my.advice; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class AnnotationAdvice { @Before("execution(* com.my.servic.impl.*.*(..))") public void before(JoinPoint jp){ System.out.println("方法调用前。。"); System.out.println(jp.getTarget()+" +++>>> "+jp.getSignature()+" "+jp.getArgs()); } @Around("execution(* com.my.servic.impl.*.*(..))") public void round(ProceedingJoinPoint pjp){ System.out.println("环绕前。。"); try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("环绕后前。。"); } }
自定义写法
package com.my.advice; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class CustomerAdvice { public void methodBefore(JoinPoint jp){ System.out.println("方法调用前。。"); System.out.println(jp.getTarget()+" +++>>> "+jp.getSignature()+" "+jp.getArgs()); } public void methodAfter(JoinPoint jp){ System.out.println("方法调用后。。"); } public void roundMethod(ProceedingJoinPoint pjp){ System.out.println("环绕前。。"); try { pjp.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("环绕后。。"); } }
常规实现接口写法
package com.my.advice; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import com.my.bean.Emp; import com.my.bean.Student; public class LogAdivce implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor { @Override public void before(Method method, Object[] arg1, Object target) throws Throwable { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("目标对象" + target.getClass().getName() + "的" + method.getName() + " 在" + sf.format(new Date()) + " 被调用了"); for (Object obj : arg1) { if (obj.getClass().getName().equals("com.my.bean.Student")) { Student stu = (Student) obj; System.out.println("参数为:" + stu.getName()); } else if (obj.getClass().getName().equals("com.my.bean.Emp")) { Emp e = (Emp) obj; System.out.println("参数为:" + e.getName()); } } } @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { // TODO Auto-generated method stub } @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("环绕前。。"); Object obj = methodInvocation.proceed(); System.out.println("环绕后。。"); return obj; } }
时间: 2024-11-05 19:29:38