spring-aop


        前置通知
        实现org.springframework.aop.MethodBeforeAdvice接口
      

package com.zking.aop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
				String targetName = arg2.getClass().getName();
				String methodName = arg0.getName();
				String params = Arrays.toString(arg1);
				String msg="【前置日志】:"+targetName+"."+methodName+",带参数为"+params;
				System.out.println(msg );
	}

}

        后置通知
        实现org.springframework.aop.AfterReturningAdvice接口
      

package com.zking.aop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.aop.AfterReturningAdvice;

public class MyAfterReturningAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg="【后置通知日志】:"+targetName+"."+methodName+",带参数为"+params+" 目标对象返回值"+ returnValue;
		System.out.println(msg );
	}

}

        环绕通知
        org.aopalliance.intercept.MethodInterceptor
     

package com.zking.aop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyMethodInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object target = invocation.getThis();
		Method method = invocation.getMethod();
		Object[] arguments = invocation.getArguments();
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(arguments);
		
		String msg="【环绕通知日志】:"+targetName+"."+methodName+",带参数为"+params;
		System.out.println(msg );
		Object returnValue = invocation.proceed();
		msg="【环绕通知日志】:"+targetName+"."+methodName+",带参数为"+params+" 目标对象返回值"+ returnValue;
		System.out.println(msg );
		return returnValue;
	}

}


        
        异常通知
        org.springframework.aop.ThrowsAdvice
        public void afterThrowing( MyException1 ex ) {}
        出现异常执行系统提示,然后进行处理。

package com.zking.aop.advice;

import org.springframework.aop.ThrowsAdvice;

import com.zking.aop.ex.PriceException;

public class MyThrowsAdvice implements ThrowsAdvice {
	public void afterThrowing( PriceException ex ) {
		System.out.println("价格异常,请重新输入!!!");
	}
}


        
        过滤通知
        org.springframework.aop.support.RegexpMethodPointcutAdvisor
        处理买书返利的bug

 

在spring-context.xml相关配置

<!-- 目标对象 -->
	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz"></bean>
	<!-- 前置通知 -->
	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean>
	<!-- 后置通知 -->
	<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfterReturningAdvice"></bean>
	<!-- 异常通知 -->
	<bean class="com.zking.aop.advice.MyThrowsAdvice" id="MyThrowsAdvice"></bean>
	<!-- 环绕通知 -->
	<bean class="com.zking.aop.advice.MyMethodInterceptor" id="myMyMethodInterceptor"></bean>
	<!-- 过滤通知(适配器) -->
	<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"
		id="myAfterReturningAdvice2">
		<property name="advice" ref="myAfterReturningAdvice"></property>
		<!-- .*buy *代表任意字符 已buy结尾的方法都会切入后置通知 -->
		<property name="pattern" value=".*buy"></property>
		<property name="patterns">
			<list>
				<value>.*buy</value>
			</list>
		</property>
	</bean>
	<!-- 生成代理 -->
	<bean class="org.springframework.aop.framework.ProxyFactoryBean"
		id="myProxyFactory">
		<property name="target" ref="bookBiz"></property>
		<property name="proxyInterfaces">
			<!-- 生成出的代理必定要实现目标对象所实现的接口 -->
			<list>
				<value>com.zking.aop.biz.BookBiz</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<!-- 前置通知 -->
				<value>myMethodBeforeAdvice</value>
				<!-- 后置通知 -->
				<value>myAfterReturningAdvice</value>
				<!-- 过滤通知 -->
				<value>myAfterReturningAdvice2</value>
				<!-- 环绕通知 -->
				<value>myMyMethodInterceptor</value>
				<!-- 异常通知 -->
				<value>MyThrowsAdvice</value>
			</list>
		</property>
	</bean>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值