AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
============================
实操:
package test.aop;
//先定义aop接口
public interface World {
void printWord();
void print();
}
两个实现类:
package test.aop;
import org.springframework.stereotype.Component;
//对aop接口的实现
@Component("h2")
public class HelloWorld1 implements World {
@Override
public void printWord() {
System.out.println("HelloWorld-1...printWorld()");
}
@Override
public void print() {
System.out.println("HelloWorld-1...print()");
return ;
}
}
横切关注点,打印时间
package test.aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
//横切关注点,打印时间
@Component("timeHandler")
public class TimeHandler {
public void printTime() {
System.out.println("当前时间是:" + System.currentTimeMillis());
}
@Test
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
HelloWorld1 hw1 = (HelloWorld1) ctx.getBean("helloWorldImpl1");
HelloWorld2 hw2 = (HelloWorld2) ctx.getBean("helloWorldImpl2");
hw1.printWord();
System.out.println("-----");
hw1.print();
System.out.println("==============");
hw2.printWord();
System.out.println("----");
hw2.print();
;
}
}
app.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- AOP依赖的loc注入 -->
<bean id="helloWorldImpl1" class="test.aop.HelloWorld1" />
<bean id="helloWorldImpl2" class="test.aop.HelloWorld2" />
<bean id="timeHandler" class="test.aop.TimeHandler" />
<bean id="logHandler" class="test.aop.LogHandler" />
<!-- 动态代理分为两种,针对接口的动态代理和针对普通类的动态代理,java中的动态代理是真的接口的动态代理,cglib是针对普通类的动态代理,目标javaEE的依赖包和Spring的jar包中已经包含了cglib相关jar包,因此即可以对代理也可以对普通类进行动态代理。 -->
<aop:config proxy-target-class="true"> <!-- 如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。 -->
<aop:aspect id="time" ref="timeHandler" order="1">
<aop:pointcut id="addAllMethod" expression="execution(* test.aop.World.*(..))" />
<aop:before method="printTime" pointcut-ref="addAllMethod" />
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect>
<aop:aspect id="log" ref="logHandler" order="2">
<aop:pointcut id="printLog" expression="execution(* test.aop.World.*(..))" />
<aop:before method="LogBefore" pointcut-ref="printLog" />
<aop:after method="LogAfter" pointcut-ref="printLog" />
</aop:aspect>
</aop:config>
</beans>