AOP术语
Joinpoint:(连接点)类里面可以被增强的方法,这些方法称为连接点
Pointcut :(切入点)类里面可以有很多方法被增强,比如实际操作中,只是增强了类里面add方法和update方法,实际增强的方法称为切入点。官方定义( 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义)
通知/增强 增强的逻辑 ,称为增强,比如扩展日志功能,这个日志功能被称为增强
Spring的aop的操作
创建spring核心配置文件(jar 和xml),在xml中导入aop的约束。
创建各个方法
public class Book {
public void add() {
System.out.println("add........");
}
}
public class MyBook {
public void before1() {
System.out.println("前置增强.....");
}
public void after1() {
System.out.println("后置增强.....");
}
// 环绕方法
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 方法之前
System.out.println("方法之前.....");
// 执行被增强的方法
proceedingJoinPoint.proceed();
// 方法之后
System.out.println("方法之后.....");
}
}
xml配置
<!-- 1 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut
expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1" />
<!-- 2.2 配置切面
把增强用到方法上面
-->
<aop:aspect ref="myBook">
<!-- 配置增强类型
method:增强类里面使用哪个方法作为前置
-->
<!-- 前置方法 -->
<aop:before method="before1" pointcut-ref="pointcut1"/>
<!-- 后置方法 -->
<aop:after-returning method="after1" pointcut-ref="pointcut1"/>
<!-- 环绕方法 -->
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
自己理解吧!多看些教程。就能理解,我发的只是个小例子。讲解自己还差点东西。