1.需求说明
有一个
SmartAnimal
接口,可以完成简单的加减法
,
要求在执行
getSum()
和
getSub()
时,输出执行前,执行过程,执行后的日志输出,请思考如何实现.
2. 解决方案-传统方式
1. 传统的解决思路,在各个方法的 [前,执行过程, 后] 输出日志提示信息
代码如下:
SmartAnimalable接口:
public interface SmartAnimalable {
//求和
float getSum(float i, float j);
//求差
float getSub(float i, float j);
}
SmartDog类:
public class SmartDog implements SmartAnimalable {
@Override
public float getSum(float i, float j) {
System.out.println("日志-方法名-getSum-参数 " + i + " " + j);
float result = i + j;
System.out.println("方法内部打印result = " + result);
System.out.println("日志-方法名-getSum-结果result= " + result);
return result;
}
@Override
public float getSub(float i, float j) {
System.out.println("日志-方法名-getSub-参数 " + i + " " + j);
float result = i - j;
System.out.println("方法内部打印result = " + result);
System.out.println("日志-方法名-getSub-结果result= " + result);
return result;
}
}
测试:
public class AopTest {
@Test
public void smartDogTest() {
SmartAnimalable smartAnimalable = new SmartDog();
smartAnimalable.getSum(10, 2);
System.out.println("=======================");
smartAnimalable.getSub(10, 2);
}
}
缺点:
来思考一下,解决方案好吗
? ===>
代码冗余
,
其实就是单个对象的调用,并没有很好的解决
3. 解决方案-动态代理方式 !!!!!!!!
1. 动态代理解决思路,在调用方法时,使用反射机制,根据方法去决定调用哪个对象方法
代码如下:
SmartAnimalable接口:
public interface SmartAnimalable {
//求和
float getSum(float i, float j);
//求差
float getSub(float i, float j);
}
SmartDog类:
public class SmartDog implements SmartAnimalable {
@Override
public float getSum(float i, float j) {
//System.out.println("日志-方法名-getSum-参数 " + i + " " + j);
float result = i + j;
System.out.println("方法内部打印result = " + result);
//System.out.println("日志-方法名-getSum-结果result= " + result);
return result;
}
@Override
public float getSub(float i, float j) {
//System.out.println("日志-方法名-getSub-参数 " + i + " " + j);
float result = i - j;
System.out.println("方法内部打印result = " + result);
//System.out.println("日志-方法名-getSub-结果result= " + result);
return result;
}
}
创建一个代理类 MyProxyProvider :
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/**
* @author jiamian
* @version 1.0
* 可以返回一个动态代理对象, 可以执行SmartDog对象的方法
*/
public class MyProxyProvider {
//定义我们要执行的目标对象, 该对象需要实现SmartAnimalable
private SmartAnimalable target_obj;
//构造器
public MyProxyProvider(SmartAnimalable target_obj) {
this.target_obj = target_obj;
}
//我们一个方法,在目标对象执行前执行
//public void before(Object proxy, Method method, Object[] args) {
// System.out.println("before-方法执行前-日志-方法名-" + method.getName() + "-参数 "
// + Arrays.asList(args)); //这里从AOP看,就是一个横切关注点-前置通知
//}
我们一个方法,在目标对象执行后执行
//public void after(Method method, Object result) {
// System.out.println("after-方法执行正常结束-日志-方法名-" + method.getName() + "-结果result= "
// + result);//从AOP看, 也是一个横切关注点-返回通知
//}
//方法, 可以返回代理对象,该代理对象可以执行目标对象
public SmartAnimalable getProxy() {
//1. 先到的类加载器/对象
ClassLoader classLoader = target_obj.getClass().getClassLoader();
//2. 得到要执行的目标对象的接口信息
Class<?>[] interfaces = target_obj.getClass().getInterfaces();
//3. 创建InvocationHandler
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
try {
//before(proxy, method, args);
MyAOP.before(proxy, method, args);//前置代码,手动封装到了MyAOP类
//使用反射调用方法
result = method.invoke(target_obj, args);
//after(method, result);
MyAOP.after(method, result);
} catch (Exception e) {
e.printStackTrace();
//如果反射执行方法时,出现异常,就会进入到catch{}
System.out.println("方法执行异常-日志-方法名-" + method.getName()
+ "-异常类型=" + e.getClass().getName());//从AOP看, 也是一个横切关注点-异常通知
} finally {//不管你是否出现异常,最终都会执行到finally{}
//从AOP的角度看, 也是一个横切关注点-最终通知
System.out.println("方法最终结束-日志-方法名-" + method.getName());
}
return result;
}
};
//创建代理对象
SmartAnimalable proxy =
(SmartAnimalable) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
return proxy;
}
}
编写一个将 前置通知/返回通知/异常通知/后置通知 封装成方法的类 MyAOP
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* @author jiamian
* @version 1.0
* 我们自己编写一个一个极简的AOP类
*/
public class MyAOP {
//我们一个方法,在目标对象执行前执行
public static void before(Object proxy, Method method, Object[] args) {
System.out.println("HspAOP-方法执行前-日志-方法名-" + method.getName() + "-参数 "
+ Arrays.asList(args)); //这里从AOP看,就是一个横切关注点-前置通知
}
//我们一个方法,在目标对象执行后执行
public static void after(Method method, Object result) {
System.out.println("HspAOP-方法执行正常结束-日志-方法名-" + method.getName() + "-结果result= "
+ result);//从AOP看, 也是一个横切关注点-返回通知
}
}
测试:
public class AopTest {
@Test
public void smartDogTestByProxy() {
SmartAnimalable smartAnimalable = new SmartDog();
MyProxyProvider myProxyProvider =
new MyProxyProvider(smartAnimalable);
//我们返回了代理对象
SmartAnimalable proxy =
myProxyProvider.getProxy();
proxy.getSum(10, 2);
System.out.println("====================");
proxy.getSub(10, 2);
}
}