package com.powernode.spring6.service;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
//切面类是需要使用@Aspect注解标注的,标注了就是一个切面
@Aspect
@Order(2)
public class LogAspect //切面
{
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
//切面等于通知加上切点
//通知等于增强,就是说要编写的增强代码
//这里的Advice以方法的形式出现(因为方法里面写代码)
//Before注解只要标注,就是一个前置通知,内部写切点表达式
@Pointcut("execution(* com.powernode.spring6.service..*(..))")
//定义通用的切点表达式
public void commonAspect()
{
//这个方法只是一个标记,方法名随意,方法体也不需要写东西
}
@Before("commonAspect()")
public void Before(JoinPoint joinPoint)
{
//这个joinPoint会在Spring容器中调用这个方法的时候自动传过来
//我们可以直接使用
Signature signature = joinPoint.getSignature();
//获取目标方法的签名,内部有方法的具体信息
logger.info(signature.getName());
logger.info("我是一段前置通知");
}
//后置通知
@AfterReturning("commonAspect()")
public void AfterReturning()
{
logger.info("我是一段后置通知");
}
//环绕通知
@Around("commonAspect()")
public void Around(ProceedingJoinPoint joinPoint) throws Throwable {
//前面的代码
logger.info("前环绕通知");
//执行目标
joinPoint.proceed();//用来执行目标
//后面的代码
logger.info("后环绕通知");
}
//最终通知
//finally语句块中的通知
@After("commonAspect()")
public void After()
{
logger.info("最终通知");
}
//异常通知
@AfterThrowing("commonAspect()")
public void AfterThrowing()
{
logger.info("异常通知");
}
}
package com.powernode.spring6.service;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
//切面类是需要使用@Aspect注解标注的,标注了就是一个切面
@Aspect
@Order(2)
public class LogAspect //切面
{
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
//切面等于通知加上切点
//通知等于增强,就是说要编写的增强代码
//这里的Advice以方法的形式出现(因为方法里面写代码)
//Before注解只要标注,就是一个前置通知,内部写切点表达式
@Pointcut("execution(* com.powernode.spring6.service..*(..))")
//定义通用的切点表达式
public void commonAspect()
{
//这个方法只是一个标记,方法名随意,方法体也不需要写东西
}
@Before("commonAspect()")
public void Before(JoinPoint joinPoint)
{
//这个joinPoint会在Spring容器中调用这个方法的时候自动传过来
//我们可以直接使用
Signature signature = joinPoint.getSignature();
//获取目标方法的签名,内部有方法的具体信息
logger.info(signature.getName());
logger.info("我是一段前置通知");
}
//后置通知
@AfterReturning("commonAspect()")
public void AfterReturning()
{
logger.info("我是一段后置通知");
}
//环绕通知
@Around("commonAspect()")
public void Around(ProceedingJoinPoint joinPoint) throws Throwable {
//前面的代码
logger.info("前环绕通知");
//执行目标
joinPoint.proceed();//用来执行目标
//后面的代码
logger.info("后环绕通知");
}
//最终通知
//finally语句块中的通知
@After("commonAspect()")
public void After()
{
logger.info("最终通知");
}
//异常通知
@AfterThrowing("commonAspect()")
public void AfterThrowing()
{
logger.info("异常通知");
}
}
package com.powernode.spring6.service;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(0)//数字越小优先级越高
public class SecurityAspect//安全模块切面
{
private static final Logger logger = LoggerFactory.getLogger(SecurityAspect.class);
@Before("com.powernode.spring6.service.LogAspect.commonAspect()")
public void before()
{
logger.info("安全模块前置通知");
}
}
package com.powernode.spring6.service;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(0)//数字越小优先级越高
public class SecurityAspect//安全模块切面
{
private static final Logger logger = LoggerFactory.getLogger(SecurityAspect.class);
@Before("com.powernode.spring6.service.LogAspect.commonAspect()")
public void before()
{
logger.info("安全模块前置通知");
}
}
package com.powernode.spring6.test;
import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAOPTest
{
@Test
public void TestBefore()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
orderService.generate();
}
}
package com.powernode.spring6.test;
import com.powernode.spring6.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAOPTest
{
@Test
public void TestBefore()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
orderService.generate();
}
}