Skip to content

Commit a35890f

Browse files
committed
[Conclusion]
1. Add conclusion to static proxy and java.lang.reflect.Proxy.
1 parent d637328 commit a35890f

File tree

1 file changed

+118
-1
lines changed

1 file changed

+118
-1
lines changed

Algorithm(4th_Edition)/JavaConclusion.txt

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,121 @@ Throw:程序员自己抛出异常的语句:
1212
throw new RuntimeException();
1313
Throws:声明方法需要抛出异常的定义:
1414
public void test() throws Exception{
15-
}
15+
}
16+
17+
---------------------------------Proxy--------------------------------------------
18+
代理是一种面向切面编程的实现AOP,我们可以通过代理对方法进行增强。分为静态代理和动态代理,而动态代理又分为API(java.lang.reflect)中的Proxy和Cglib,后两种用于Spring。
19+
1.静态代理:
20+
静态代理在使用时,需要定义接口或者父类,被代理对象与代理对象一起实现相同的接口或者是继承相同父类.
21+
1.定义一个要实现方法的接口:
22+
public interface IUserDao {
23+
24+
void save();
25+
}
26+
2.定义一个目标对象:
27+
public class UserDao implements IUserDao {
28+
public void save() {
29+
System.out.println("----已经保存数据!----");
30+
}
31+
}
32+
3.定义一个代理对象,在构造函数中接收第二步生成的目标对象,并在代理类中调用目标对象实现方法,并且实现增强。
33+
public class UserDaoProxy implements IUserDao{
34+
//接收保存目标对象
35+
private IUserDao target;
36+
public UserDaoProxy(IUserDao target){
37+
this.target=target;
38+
}
39+
40+
public void save() {
41+
System.out.println("开始事务...");
42+
target.save();//执行目标对象的方法
43+
System.out.println("提交事务...");
44+
}
45+
}
46+
4.在调用方法时不直接调用对象,而是通过静态代理类调用方法。
47+
public class App {
48+
public static void main(String[] args) {
49+
//目标对象
50+
UserDao target = new UserDao();
51+
52+
//代理对象,把目标对象传给代理对象,建立代理关系
53+
UserDaoProxy proxy = new UserDaoProxy(target);
54+
55+
proxy.save();//执行的是代理的方法
56+
}
57+
}
58+
总结:
59+
1.可以实现方法的横向增强。
60+
2.要维护很多实现类。
61+
62+
2.动态代理:
63+
1.java.lang.reflect实现:
64+
这种方法通过调用java.lang.reflect中的Proxy方法实现:
65+
1.定义一个接口:
66+
public interface ProxyTestInterface {
67+
public void test() throws Exception;
68+
}
69+
2.定义该接口的实现类:
70+
public class ProxyTestImpl implements ProxyTestInterface {
71+
@Override
72+
public void test() throws Exception {
73+
System.out.println("test");
74+
}
75+
}
76+
3.通过Proxy中的newProxyInstance方法创建一个proxy对象:
77+
public class ProxyFactory {
78+
private static ProxyTestInterface instance = new ProxyTestImpl(); //在invoke方法中需要一个实现类对象,所以通过新建,或是接收参数的方法传入一个实现类对象。
79+
@SuppressWarnings("rawtypes")
80+
public static ProxyTestInterface getProxy(Class iterface){
81+
// newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
82+
// loader:生成当前代理的类的加载器。
83+
// interfaces:生成的代理类的接口的数组。
84+
// h:对代理类调用的Handler
85+
return (ProxyTestInterface)Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{iterface}, new InvocationHandler() {
86+
87+
@Override
88+
public Object invoke(Object proxy, Method method, Object[] args)
89+
throws Throwable {
90+
if(method.getName().contains("test")){ //选择Jointpoint
91+
System.out.println("before advice"); //对代理实现了增强
92+
return method.invoke(instance, args); //通过反射实现需要增强的方法。
93+
}
94+
return method.invoke(instance, args); //不需要增强的方法放行(不一定,根据业务逻辑放行或是拦截。)
95+
}
96+
});
97+
}
98+
}
99+
4.调用代理类:
100+
public static void main(String[] args) throws Exception {
101+
Object proxy = ProxyFactory.getProxy(ProxyTestInterface.class);
102+
((ProxyTestInterface)proxy).test();
103+
}
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+

0 commit comments

Comments
 (0)