xml方式实现AOP
一、切点表达式

1. execution(void com.itheima.aop.Target.method())不常用
匹配com.itheima.aop.Target类中无参、返回类型为 void的 method()方法。
2. execution(void com.itheima.aop.Target.*(..))
匹配 com.itheima.aop.Target类中任意参数、返回类型为 void的所有方法。
3. execution(* com.itheima.aop.*.*(..))最常用
匹配com.itheima.aop 包下任意类、任意返回方法、任意参数。
4. execution(* com.itheima.aop..*.*(..))
匹配 com.itheima.aop 包及其任意子包下,任意类、任意返回值、任意参数的所有公有方法。
5. execution(* *..*.*(..))
匹配任意包、任意类、任意返回值、任意参数的所有公有方法(即“全扫”)。

二、通知类型

三、切点表达式的抽取


四、基于注解的AOP开发

上个是从xml里配置,这个是在代码里通过注释配置

五、注解汇总
@Component("myAspect") :Spring 通用型组件注解,把当前类注册成 Bean, id=myAspect 。
配合 <context:component-scan/> 或 @SpringBootApplication 扫描即可;
后面在 XML 或别的类里可直接 @Autowired MyAspect xxx 。
注意
如果只写 @Component 不给名字,默认 Bean id 是类名首字母小写 myAspect 。
2. @Aspect :告诉 Spring 这是一个切面类,才会去解析里面的 @Before/@Around 等通知。
怎么用
必须与 @Component 同时出现(或 XML 里配 <bean> ),否则 Spring 根本扫描不到。
注意
纯 Java 配置时记得加 @EnableAspectJAutoProxy或<aop:aspectj-autoproxy/> ,否则 @Aspect 失效 。
@Component("myAspect") // 1. 先让 Spring 扫描到并变成 Bean
@Aspect // 2. 再告诉 Spring“这是个切面”
public class LogAspect {// 定义切点@Pointcut("execution(* com.example.service.*.*(..))")public void pt() {}// 前置通知@Before("pt()")public void before(JoinPoint jp) {System.out.println("【切面】方法开始 -> " + jp.getSignature().getName());}
}
