AOP简介
1 AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统OOP的补充。
2.AOP的主要编程对象时切面(aspect),而切面模块化横切关注点。
3.AOP的好处:
每个事物逻辑位于一个位置,代码不分散,便于维护和升级
业务模块更简洁,只包含核心业务代码
AOP术语
切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
通知(Advice):切面必须要完成的工作
目标(Target):被通知的对象
代理(Proxy):向目标对象应用通知后创建的对象
连接点(Joinpoint):程序执行的某个特定位置,如类某个方法调用前、调用后、方法抛出异常后等。
切点(pointcut):每个类都拥有多个连接点

下例中模拟计算器加减乘除方法,在方法前后要加上处理。使用AOP
计算器接口:
package com.hcx.aop.helloworld;
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i , int j);
int div(int i , int j);
}
计算器实现类:package com.hcx.aop.helloworld;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result= i+j;
return result;
}
@Override
public int sub(int i, int j) {
int result= i-j;
return result;
}
@Override
public int mul(int i, int j) {
int result= i*j;
return result;
}
@Override
public int div(int i, int j) {
int result= i/j;
return result;
}
}
/**
* 使用动态代理完成AOP
*
*/
public class ArithmeticCalculatorLoggingProxy {
// 要代理的对象
private ArithmeticCalculator target;
//构造器,传入需要代理的对象
public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
super();
this.target = target;
}
// 返回代理对象:计算器
public ArithmeticCalculator getLoggingProxy() {
// 代理的计算器,声明个对象
ArithmeticCalculator proxy = null;
// 获取计算器类加载器
ClassLoader loader = target.getClass().getClassLoader();
// 代理对象的类型,有哪些方法
@SuppressWarnings("rawtypes")
Class[] interfaces = new Class[] { ArithmeticCalculator.class };
//当调用代理对象的方法时,该代码执行
InvocationHandler h = new InvocationHandler() {
/**
* proxy: 代理对象。 一般不使用该对象
* method: 正在被调用的方法
* args: 调用方法传入的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
// 打印日志
System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
// 调用目标方法
Object result = null;
try {
// 前置通知
result = method.invoke(target, args);
// 返回通知, 可以访问到方法的返回值
} catch (NullPointerException e) {
e.printStackTrace();
// 异常通知, 可以访问到方法出现的异常
}
// 后置通知. 因为方法可以能会出异常, 所以访问不到方法的返回值
// 打印日志
System.out.println("[after] The method ends with " + result);
return result;
}
};
/**
* loader: 代理对象使用的类加载器。 interfaces: 指定代理对象的类型. 即代理代理对象中可以有哪些方法. h:
* 当具体调用代理对象的方法时, 应该如何进行响应, 实际上就是调用 InvocationHandler 的 invoke 方法
*/
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
Main方法:
<pre name="code" class="java">package com.hcx.aop.helloworld;
public class Main {
public static void main(String[] args) {
//
// ArithmeticCalculator arithmeticCalculator = null;
// arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();
//
ArithmeticCalculator target = new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
int result = proxy.add(1, 2);
System.out.println("-->" + result);
result = proxy.div(4, 2);
System.out.println("-->"+ result);
}
}