1
2
3
4
5
6
7
8
|
package
com.mickeymouse.jdk_proxy;
//增删改查
public
interface
UserDao {
public
abstract
void
add();
public
abstract
void
update();
public
abstract
void
delect();
public
abstract
void
select();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.mickeymouse.jdk_proxy;
public
class
UserDaoimpl
implements
UserDao {
public
void
add(){
System.out.println(
"添加成功"
);
};
public
void
update(){
System.out.println(
"修改成功"
);
};
public
void
delect(){
System.out.println(
"删除成功"
);
};
public
void
select(){
System.out.println(
"查询成功"
);
};
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.mickeymouse.jdk_proxy;
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Method;
import
java.lang.reflect.Proxy;
public
class
JdkProxy {
public
static
UserDao getUserDao(
final
UserDao dao){
UserDao userDao = (UserDao) Proxy.newProxyInstance(JdkProxy.
class
.getClassLoader(), dao.getClass().getInterfaces(),
new
InvocationHandler() {
public
Object invoke(Object proxy, Method method, Object[] args)
throws
Throwable {
System.out.println(
"=======>前"
);
Object object = method.invoke(dao, args);
System.out.println(
"=======>后"
);
return
object;
}
});
return
userDao;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@Test
public
void
test1(){
//普通实例
UserDao use =
new
UserDaoimpl();
//生成动态代理对象
UserDao userDao = JdkProxy.getUserDao(use);
//执行方法
userDao.add();
userDao.delect();
userDao.select();
userDao.update();
}
|
实现类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 订单管理的DAO的类
*
*/
public
class
OrderDao {
public
void
save(){
System.out.println(
"保存订单..."
);
}
public
void
update(){
System.out.println(
"修改订单..."
);
}
public
void
delete(){
System.out.println(
"删除订单..."
);
}
public
void
find(){
System.out.println(
"查询订单..."
);
}
}
|
切面类:
1
2
3
4
5
6
7
8
9
|
public
class
MyAspect {
public
void
myBefore(){
System.out.println(
"======>前"
);
}
public
void
myAfter(){
System.out.println(
"======>后"
);
}
}
|
代理类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public
static
UserService createService() {
//目标类 类 = new 类
final
UserService userService =
new
UserService();
//切面类
final
MyAspect myAspect =
new
MyAspect();
//代理类 --cglib (代理类 和 目标类 父子关系)
// 创建核心类(Enhancer 增强)
Enhancer enhancer =
new
Enhancer();
// 确定父类
enhancer.setSuperclass(userService.getClass());
// 设置回调,类似JDK中InvocationHandler
// ** CallBack 接口处理所有内容
// ** 子接口 MethodInterceptor 用于处理方法
enhancer.setCallback(
new
MethodInterceptor() {
/**
* intercept() 等效 jdk invoke()方法
* * 参数1、参数2、参数3:与invoke一样的
* * 参数4:methodProxy
*/
@Override
public
Object intercept(Object proxy, Method method, Object[] args,
MethodProxy methodProxy)
throws
Throwable {
//前
myAspect.myBefore();
//执行目标类方式
Object obj = method.invoke(userService, args);
// *** 执行代理类的父类--目标类
methodProxy.invokeSuper(proxy, args);
//后
myAspect.myAfter();
return
obj;
}
});
// 3.4 创建代理
UserService proxyService = (UserService)enhancer.create();
return
proxyService;
}
|
测试类:
1
2
3
4
5
6
|
@Test
public
void
demo02(){
UserService userService = MyFactory.createService();
userService.addUser();
userService.updateUser();
}
|