# AOP中的代理模式应用
# Spring AOP实现原理
- Spring基于代理模式实现功能动态扩展,包含两种形式:
- 目标类用于接口,通过JDK动态代理实现功能扩展
- 目标类没有接口,通过CGLib组件实现功能扩展
# 7代理模式与静态代理
# 代理模式
- 代理模式通过代理对象对原对象的实现功能扩展
- 代理类与实现类需要实现相同的接口
- 代理类需要持有委托类的对象
- 静态代理是指:必须手动创建代理类的代理模式的使用方法
- 静态代理的缺陷:每一次的扩展都需要手动创建代理类
# 静态代理示例代码:
package com.torey.spring.aop.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/2 17:32
* @描述:
*/
public interface UserService {
void createUser();
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
package com.torey.spring.aop.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/2 17:32
* @描述:
*/
public class UserServiceImpl implements UserService {
public void createUser() {
System.out.println(this.getClass().getName() + ".createUser()");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.torey.spring.aop.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/2 17:35
* @描述: 代理类的特点:持有委托类的对象
*/
public class UserServiceProxy implements UserService {
private UserService userService;
public UserServiceProxy(UserService userService) {
this.userService = userService;
}
public void createUser() {
System.out.println(this.getClass().getName() + ".createUser():前置通知");
this.userService.createUser();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.torey.spring.aop.service;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/2 17:35
* @描述: 代理类的特点:持有委托类的对象
*/
public class UserServiceProxy2 implements UserService {
private UserService userService;
public UserServiceProxy2(UserService userService) {
this.userService = userService;
}
public void createUser() {
this.userService.createUser();
System.out.println(this.getClass().getName() + ".createUser():后置通知");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.torey.spring.aop;
import com.torey.spring.aop.service.UserService;
import com.torey.spring.aop.service.UserServiceImpl;
import com.torey.spring.aop.service.UserServiceProxy;
import com.torey.spring.aop.service.UserServiceProxy2;
/**
* @Author http://torey611.gitee.io/li-tao-feng/
* @Email torey6061@qq.com
* @Date 2021/5/2 17:39
* @描述:
*/
public class Application {
public static void main(String[] args) {
UserService userService= new UserServiceProxy2(new UserServiceProxy(new UserServiceImpl()));
userService.createUser();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
执行结果如下:
com.torey.spring.aop.service.UserServiceProxy.createUser():前置通知
com.torey.spring.aop.service.UserServiceImpl.createUser()
com.torey.spring.aop.service.UserServiceProxy2.createUser():后置通知
1
2
3
2
3
# 导航,上一页,下一页
6利用注解配置Spring AOP
8AOP的底层原理-JDK的动态代理
# 支持我-微信扫一扫-加入微信公众号
# 赞赏作者
# 种一棵树,最好的时间是十年前,其次是现在
立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?