切面编程
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("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("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("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("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("运行异常");
}
}
}