标签:
一、AOP基础
1.基本需求
需求: 日志功能,在程序执行期间记录发生的活动。
ArithmeticCalculate.java
public interface ArithmeticCalculate{
public int add(int a,int b);
public int sub(int a,int b);
public int mul(int a,int b);
public int div(int a,int b);
}
ArithmeticCalculateImpl.java
public class ArithmeticCalculateImpl implements ArithmeticCalculate{
@Override
public int add(int a,int b){
System.out.println("The method add.....begin");
int result = a + b;
System.out.println("The method add.....end");
return result;
}
@Override
public int sub(int a,int b){
System.out.println("The method sub.....begin");
int result = a - b;
System.out.println("The method sub.....end");
return result;
}
@Override
public int mul(int a,int b){
System.out.println("The method mul.....begin");
int result = a * b;
System.out.println("The method mul.....end");
return result;
}
@Override
public int div(int a,int b){
System.out.println("The method div.....begin");
int result = a / b;
System.out.println("The method div.....end");
return result;
}
}
以上这样写会出现两种问题。
(1)代码混乱
越来越多的非业务需求加入后,原有的业务方法急剧膨胀。每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点。
(2)代码分散
以日志需求为例,只是为了满足这个单一需求,就不得不在多个模块重复相同的代码日志,如果日志需求发生改变还得修改所有的需求。
使用动态代理解决以上问题
原理: 使用一个代理将对象包装起来,然后改代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。
ArithmeticCalculateProxy.java
public class ArithmeticCalculateProxy{
//要代理的对象
private ArithmeticCalculate target;
public ArithmeticCalculateProxy(){
}
public ArithmeticCalculateProxy(ArithmeticCalculate target){
this.target = target;
}
public ArithmeticCalculate getProxy(){
ArithmeticCalculate proxy = null;
//代理对象由哪一个类加载器负责加载
ClassLoader loader = target.getClass().getClassLoader();
//代理对象的类型,即有哪些方法
Class[] interfaces = new Class[]{ArithmeticCalculate.class};
//当调用代理对象其中方法时,该执行的代码
InvocationHandler handler = new InvocationHandler(){
/*
* proxy: 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用
* method: 正在被调用的方法
* args:调用方法时传入的参数
*/
@Override
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
String methodName = method.getName();
//日志
System.out.println("The method " + methodName +" begin......");
//执行方法
Object result = method.invoke(target,args);
//日志
System.out.println("The method " + methodName +" end......");
return result;
}
};
proxy = (ArithmeticCalculate)Proxy.newProxyInstance(loader,interfaces,handler);
return proxy;
}
}
Test.java
@Test
public void testCalculate(){
ArithmeticCalculate target = new ArithmeticCalculateImpl();
ArithmeticCalculate proxy = new ArithmeticCalculateProxy(target).getProxy();
System.out.println(proxy.add(4,2));
System.out.println(proxy.sub(4,2));
}
结果:
The method add begin......
The method add end......
6
The method sub begin......
The method sub end......
2
标签:
原文地址:http://www.cnblogs.com/yangang2013/p/5557669.html