欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring aop注解开发

程序员文章站 2022-07-12 23:15:08
...
package com.swt.aspect;
/**
 * 切面类:注解切面
 * @author cheng
 *
 */

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;

@Component
@Aspect
public class AsPectAnnotation {
	
	@Pointcut(value="execution(* com.swt.dao.impl.ProductImpl.add(..))")
	private void point1() {
		
	}
	
	@Before("AsPectAnnotation.point1()")//前置通知
	public void before() {
		System.out.println("前置通知");
	}
	
	@AfterReturning(value="execution(* com.swt.dao.impl.ProductImpl.delete(..))",returning="result")
	public void delete(Object result) {
		System.out.println(result);
		System.out.println("后置通知");
	}
	
	@Around("execution(* com.swt.dao.impl.ProductImpl.update(..))")
	public void delete(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前");
		joinPoint.proceed();
		System.out.println("环绕后");
	}
	
	@AfterThrowing(value="execution(* com.swt.dao.impl.ProductImpl.find(..))",throwing="e")
	public void find(Throwable e) throws Throwable {
		System.out.println(e);
		System.out.println("异常抛出了");
	}
	
	@After(value="execution(* com.swt.dao.impl.ProductImpl.find(..))")
	public void find2() throws Throwable {
		System.out.println("最终通知");
	}
}