一、开发步骤
1、导入AOP相关坐标
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.4</version></dependency>
2、创建目标接口和目标类(内部有切点)
3、创建切面类(内部有增强方法)
4、将目标类和切面类的对象创建权交给spring
5、在applicationC ontext.xml中配置织入关系
<!--目标对象--><bean id="target" class="com.demo.aop.Target"></bean><!--切面对象--><bean id="myAspect" class="com.demo.aop.MyAspect"></bean><!-配置织入:告诉spring框架哪些方法(切点)需要进行哪些增强(前置、后置...)--><aop:config><!--声明切面--><aop:aspect ref="myAspect"><!--切面:切点通知-->_<aop:before method="before" pointcut="execution(public void com.demo.aop.Target.save())" /></aop:aspect></aop:config>
6、测试代码
二、切点表达式
表达式语法:execution([修饰符]返回值类型包名类名方法名(参数))
- 访问修饰符可以省略
- 返回值类型、包名、类名、方法名可以使用星号*代表任意
- 包名与类名之间一 个点.代表当前包下的类,两个点..表示当前包及子包下的类
- 参数列表可以使用两个点.. 表示任意个数,任意类型的参数列表
e.g.
execut ion (public void com. ithe ima. aop. Target . method() )execution (void com. itheima.aop.Target.*(..))execution(* com. itheima.aop. *.*(..) )execution(* com. itheima.aop. .*.* (..))execution(* *. .*.*(..) )
三、通知类型

通知的配置语法:
<aop:通知类型 method="切面类中方法名" pointcut="切点表达式"></aop:通知类型>
四、切点表达式的抽取
<aop:config><!--声明切面--><aop:aspect ref="myAspect"><!--抽取切点表达式--><aop:pointcut id="myPointcut" expression="execution(* com.demo.aop.*.*(..))"></aop:pointcut><!--切面:切点通知--><aop:around method="around" pointcut-ref="myPointcut"/><aop:after method="after" pointcut-ref="myPointcut"/></aop:aspect></aop:config>
