【无标题】

切面编程

package com.project.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;

/**
 * 切面等于通知加上切点
 */
@Component("loginAspectBean")
/**
 * 标注之后他才是一个切面
 */
@Aspect
@Order(2)
//数字越小优先级越高
public class AspectAdvice {
	//	@Before("execution(修饰符号 返回值类型  全限定类名 方法名 异常)")
//	@Before("execution(* com.project.Service.UserService.login(..))")
//	@Before("execution(* com.project.Service.UserService..*(..))")
//	@Before("execution(* com.project.Service..*(..))")
	@Before("pointCut()")
	public void beforeAdvice(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		System.out.println("前置通知!!!");
	}
	
	//	后置通知
	@AfterReturning("pointCut()")
	public void AfterReturning(JoinPoint joinPoint) {
		System.out.println("后置通知!!");
	}
	
	//	环绕通知
	@Around("pointCut()")
	public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("前环绕!!");
		
		joinPoint.proceed();//执行目标  执行方法
//		没有这个的话连方法都执行不了
		System.out.println("后环绕!!!");
	}
	
	//	最终通知
	@After("pointCut()")
	public void AfterAdvice(JoinPoint joinPoint) {
		System.out.println("最终通知!!!");
	}
	
	//	异常通知
//	@AfterThrowing("execution(* com.project.Service..*(..)))")
	@AfterThrowing("pointCut()")
	public void AfterThrowing(JoinPoint joinPoint) {
		System.out.println("异常通知!!");
	}
//	让这个代码得到复用通用切点表达式
	@Pointcut("execution(* com.project.Service..*(..)))")
	public void pointCut()
	{
	}
}

package com.project.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;

/**
 * 切面等于通知加上切点
 */
@Component("loginAspectBean")
/**
 * 标注之后他才是一个切面
 */
@Aspect
@Order(2)
//数字越小优先级越高
public class AspectAdvice {
	//	@Before("execution(修饰符号 返回值类型  全限定类名 方法名 异常)")
//	@Before("execution(* com.project.Service.UserService.login(..))")
//	@Before("execution(* com.project.Service.UserService..*(..))")
//	@Before("execution(* com.project.Service..*(..))")
	@Before("pointCut()")
	public void beforeAdvice(JoinPoint joinPoint) {
		Signature signature = joinPoint.getSignature();
		System.out.println("前置通知!!!");
	}
	
	//	后置通知
	@AfterReturning("pointCut()")
	public void AfterReturning(JoinPoint joinPoint) {
		System.out.println("后置通知!!");
	}
	
	//	环绕通知
	@Around("pointCut()")
	public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("前环绕!!");
		
		joinPoint.proceed();//执行目标  执行方法
//		没有这个的话连方法都执行不了
		System.out.println("后环绕!!!");
	}
	
	//	最终通知
	@After("pointCut()")
	public void AfterAdvice(JoinPoint joinPoint) {
		System.out.println("最终通知!!!");
	}
	
	//	异常通知
//	@AfterThrowing("execution(* com.project.Service..*(..)))")
	@AfterThrowing("pointCut()")
	public void AfterThrowing(JoinPoint joinPoint) {
		System.out.println("异常通知!!");
	}
//	让这个代码得到复用通用切点表达式
	@Pointcut("execution(* com.project.Service..*(..)))")
	public void pointCut()
	{
	}
}

package com.project.Service;

import org.springframework.stereotype.Service;

@Service("userServiceBean")
public class UserService {
	public void login()
	{System.out.println("系统正在登录!!!");
		if (1 == 1) {
			throw new RuntimeException("运行异常");
		}
		
	}
	public void loginout()
	{	System.out.println("退出系统!!!");
		if (1 == 1) {
			throw new RuntimeException("运行异常");
		}
	
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值