Java???????????
???????????? ???????[ 2015/10/10 13:32:32 ] ??????????????? ???????????
????????
?????????????????????????????????????????????????????????????????????“???”??????????Щ????????漰??“???”???????????????????????м????????
????Java???????????????????????????????????????????????????????????????á????????????????????е????????????????????????(InvocationHandler)???????????????????????????????????????????
????Java??????????????????????????й??????????????????μ?????Java????????????
???????
????Spring????AOP?????????????Java????????????????????????????????????????????????????????????????????????????????????Java???????APIλ??java.lang.reflect???£??????????????漰??????????????裺
????1.???InvocationHandler???
????InvocationHandler???????????????????
????public Object invoke(Object proxy?? Method method?? Object[] args?? throws Throwable;
????proxy?????????
????method????????????????
????args???????????????????
????proxy?????????????????????????????????????????????????????£???????????????????InvocationHandler?????????б?????????????????????????????????е???????????InvocationHandler????invoke?????????invoke????????????????????method??????????????????????????????????????????????????Щ???????д???????????????invoke??????????????????????????method??????????????????????????
????2.???Proxy????????????????
????Proxy?????ж??????????????????????????newProxyInstance?????????????????????
????public static Object newProxyInstance(ClassLoader loader?? Class<?>[] interfaces?? InvocationHandler h)
????throws IllegalArgumentException
????loader?????????(??????????????????????л???????????)
????interfaces??????????????????б?(????????????????????б?)
????h??InvocationHandler??????????
????newProxyInstance????????????????б?interfaces????????ж???????????Proxy????????????????????б?interfaces?????з???????????????????????????????????????????????????????????????????????з??????????????????????????h??invoke??????
????Java??????????????????????????????????newProxyInstance??????????????????????????????????????????????÷????????????????????????????????????????????????????????InvocationHandler?е?invoke????????invoke?????????????????????y??з???????????????????????????
???????
??????????????????????????????????????????
?????????
????public class User {
????private String name;
????public User(String name) {
????super();
????this.name = name;
????}
????public String getName() {
????return name;
????}
????public void setName(String name) {
????this.name = name;
????}
????}
???????
????public interface UserDao {
????void addUser(User user);
????void deleteUser(String name);
????void updateUser(String name);
????}
?????????
public class UserDaoImpl implements UserDao {
@Override
public void addUser(User user) {
System.out.println("add user named " + user.getName() + "...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void deleteUser(String name) {
System.out.println("delete user named " + name + "...");
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void updateUser(String name) {
System.out.println("update user named " + name + "...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11