spring4 aop annotation

package com.zrd.aop.annotation;
/**
 * 第一步:定义一个接口
 * 
 * @author ZRD
 *
 */
public interface IMyService {

	int add(int i, int j);
}

package com.zrd.aop.annotation;

import org.springframework.stereotype.Component;

/**
 * 第二步:定义接口的实现类
 * 
 * @author ZRD
 *
 */
@Component
public class MyServiceImpl implements IMyService {

	@Override
	public int add(int i, int j) {
		int result = i +j;
		System.out.println("add 正在执行... " + result);
		return result;
	}

}

package com.zrd.aop.annotation;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 第三步:声明一个切面
 * 
 * 记录日记的切面
 * 
 * @author ZRD
 *
 */
@Aspect
@Component
public class LogAspect {

	/**
	 * 切入点:表示在哪个类的哪个方法进行切入。配置有切入点表达式
	 */
	@Pointcut("execution(* com.zrd.aop.annotation.*.*(..))")
	public void pointcutExpression() {
		
	}
	
	/**
	 * 1 前置通知
	 * @param joinPoint
	 */
	@Before("pointcutExpression()")
	public void beforeMethod(JoinPoint joinPoint) {
		System.out.println("前置通知执行了");
	}
	
	/**
	 * 2 后置通知
	 */
	@After("pointcutExpression()") // 在方法执行之后执行的代码. 无论该方法是否出现异常
	public void afterMethod(JoinPoint joinPoint) {
		System.out.println("后置通知执行了,有异常也会执行");
	}
	
	/**
	 * 3 返回通知
	 * 
	 * 在方法法正常结束受执行的代码
	 * 返回通知是可以访问到方法的返回值的!
	 * 
	 * @param joinPoint
	 * @param returnValue
	 * 
	 */
	@AfterReturning(value = "pointcutExpression()", returning = "returnValue")
	public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) {
		System.out.println("返回通知执行,执行结果:" + returnValue);
	}
	
	/**
	 * 4 异常通知
	 * 
	 * 在目标方法出现异常时会执行的代码.
	 * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
	 * 
	 * @param joinPoint
	 * @param e
	 */
	@AfterThrowing(value = "pointcutExpression()", throwing = "e")
	public void afterThrowingMethod(JoinPoint joinPoint, Exception e){
		System.out.println("异常通知, 出现异常 :" + e);
	}
	
	/**
	 * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. 
	 * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
	 * 且环绕通知必须有返回值, 返回值即为目标方法的返回值
	 */
	
	@Around("pointcutExpression()")
	public Object aroundMethod(ProceedingJoinPoint pjd){
		
		Object result = null;
		String methodName = pjd.getSignature().getName();
		
		try {
			//前置通知
			System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
			//执行目标方法
			result = pjd.proceed();
			//返回通知
			System.out.println("The method " + methodName + " ends with " + result);
		} catch (Throwable e) {
			//异常通知
			System.out.println("The method " + methodName + " occurs exception:" + e);
			throw new RuntimeException(e);
		}
		//后置通知
		System.out.println("The method " + methodName + " ends");
		
		return result;
	}
	
	
}

<!-- 第四步:spring配置文件:配置IOC自动扫描和AOP切面自己生产目标代理对象 -->

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<context:component-scan base-package="com.zrd.aop.*"></context:component-scan>
	
	<!-- 使用注解配置切面 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
	<!-- 使用XML配置切面 -->
	<!-- 
	<aop:config>
		<aop:pointcut expression="" id=""/>
		<aop:aspect>
			<aop:after method=""/>
		</aop:aspect>
	</aop:config> 
	-->
	
</beans>


package com.zrd.aop.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 第五步:测试
 *  
 * @author ZRD
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ApplicationContext ax = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		/** 使用切面时,一定要这样写,不然无法注入bean **/
		//IMyService service = ax.getBean(MyServiceImpl.class);
		IMyService service = (IMyService) ax.getBean("myServiceImpl");
	
		service.add(2, 8);
	}

}

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章