AOP注解配置
注解开发AOP制作步骤
◆ 导入坐标(伴随spring-context坐标导入已经依赖导入完成)
◆ 开启AOP注解支持 aspectj-autoproxy标签或者@EnableAspectJAutoProxy
◆ 配置切面 @Aspect
◆ 定义专用的切入点方法,并配置切入点 @Pointcut
◆ 为通知方法配置通知类型及对应切入点 @Before,@after, @around,@afterReturning,@AfterThrowing
@Configuration
@ComponentScan("com.it")
@EnableAspectJAutoProxy //开启注解驱动支持 代替<context:component-scan base-package="com.it"/>
public class SpringConfig {
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component //将切面创建Bean并放入IOC容器中
@Aspect
public class AOPAdvice {
@Pointcut("execution(* com.it.service.impl.UserServiceImpl.save(..))")
public void pt(){
}
//@Before("pt()")
public void before(){
System.out.println("前置before...");
}
@After("pt()")
public void after(){
System.out.println("后置after...");
}
@AfterReturning("pt()")
public void afterReturing(){
System.out.println("返回后afterReturing...");
}
@AfterThrowing("pt()")
public void afterThrowing(){
System.out.println("抛出异常后afterThrowing...");
}
@Around("pt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前around before...");
Object ret = pjp.proceed();
System.out.println("环绕后around after...");
return ret; }
}