配置xml文件
定义扫描注解的包
<context:component-scan base-package="org.ccit.com.*"></context:component-scan>
配置aspectj 自动代理 注解生效
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
编写,注解切面类
类配置两个注解 一个bean注解 一个切面注解
@Component
@Aspect
声明一个公共切入点
可以省略多次写 execution表达式 直接通知注解中引用
@Pointcut("execution(* org.ccit.com.service.UserServiceImpl.*(..))")
public void myPointcut(){
}
@Around("myPointcut()") //可用可不用
前置通知
@Before("execution(* org.ccit.com.service.UserServiceImpl.*(..))")
后置通知
returning 参数 返回 service执行后的返回值
@AfterReturning(pointcut = "execution(* org.ccit.com.service.UserServiceImpl.*(..))",returning = "retValue")
环绕通知
@Around("myPointcut()")
最终通知
@After("myPointcut()")
异常通知
参数 throwing 返回发生的异常
@AfterThrowing(pointcut = "myPointcut()",throwing = "e")
package org.ccit.com.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @program: Spring02
* @description
* @author: LIANG
* @create: 2021-04-26 12:08
**/
@Component
@Aspect
public class MyAspect03 {
//声明一个公共得切入点
@Pointcut("execution(* org.ccit.com.service.UserServiceImpl.*(..))")
public void myPointcut(){
}
@Before("execution(* org.ccit.com.service.UserServiceImpl.*(..))")
public void before(JoinPoint jp){
//获取切入点名字
System.out.println(jp.getSignature().getName());
System.out.println("前置通知");
}
/**
* @Description:
* @params: jp, retValue:service方法的返回值
* @return: void
* @Author: LIANG
* @Date: 2021/5/1 16:03
*/
//@AfterReturning(pointcut = "execution(* org.ccit.com.service.UserServiceImpl.*(..))",returning = "retValue")
@AfterReturning(pointcut = "myPointcut()",returning = "retValue")
public void after(JoinPoint jp,Object retValue){
System.out.println("后置通知");
//后置通知获取 service方法执行后的返回值
System.out.println(retValue);
}
//ProceedingJoinPoint
@Around("myPointcut()")
public Object arround(ProceedingJoinPoint pjp) throws Throwable {
String name=pjp.getSignature().getName();//返回切入点方法名
System.out.println("环绕通知_放行前"+name);
//放行
Object proceed = pjp.proceed();
System.out.println("环绕通知_放行后"+name);
return proceed;
}
/*ProceedingJoinPoint 可以放行
* JoinPoint 没有放行*/
@After("myPointcut()")
public void after01(JoinPoint jp){
System.out.println("最后通知");
}
@AfterThrowing(pointcut = "myPointcut()",throwing = "e")
public void myAfterThrowing(JoinPoint jp,Throwable e){
System.out.println("异常通知"+jp.getSignature().getName()+" 异常:"+e.getMessage());
}
}
编写,注解业务类
service注解
@Service
package org.ccit.com.service;
import org.springframework.stereotype.Service;
/**
* @program: Spring02
* @description
* @author: LIANG
* @create: 2021-04-26 11:59
**/
@Service
public class UserServiceImpl implements IUserService {
@Override
public void addUser() {
System.out.println("添加用户");
}
@Override
public void updateUser() {
System.out.println("更新用户");
}
@Override
public void deleteUser() {
System.out.println("删除用户");
}
}
编写测试类
@Test
public void method01(){
//获取Spring容器中代理对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean03.xml");
IUserService userService = (IUserService)context.getBean(IUserService.class);
userService.deleteUser();
}