AOP 案例

测量业务层接口万次执行效率

  • AOP 概念:AOP

一、 搭建环境

一、配置 Pom 坐标

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.13.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>cn.pangcy</groupId>
<artifactId>spring-book</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.14</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.13.RELEASE</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<!-- spring 整合 mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.13.RELEASE</version>
</dependency>
</dependencies>

二、Domain 层实现

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
public class User {
private String Host;
private String user;

public String getHost() {
return Host;
}

public void setHost(String host) {
Host = host;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}


@Override
public String toString() {
return "User{" +
"Host='" + Host + '\'' +
", user='" + user + '\'' +
'}';
}
}

三、Dao 层实现

1
2
3
4
public interface UserDao {
@Select("select * from user")
List<User> findAll();
}

四、Service 层实现

UserService

1
2
3
4
5
public interface UserService {
void save();
void update();
void findAll();
}

UserServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

public List<User> findAll(){
return userDao.findAll();
}

public void save(){
System.out.println("User Service Save ...");
}

public void update(){
System.out.println("User Service Update ...");
}
}

五、SpringConfig

1
2
3
4
5
@Configuration
@ComponentScan("cn.pangcy")
@EnableAspectJAutoProxy
public class SpringConfig {
}

二、AOP 实现

/aop/MyAdvice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
@Aspect
public class MyAdvice(){
@Pointcut("execution(* cn.pangcy.service.*Service.*())")
private void servicePt();

@Around("servicePt")
public void runSpeed(ProceedingJoinPoint pjp) throws Trhowable {
long start = System.currentTimeMillis();
for(int i = 0; i < 100; i++){
pjp.proceed();
}
long end = System.curretTimeMillis();
System.out.println("业务层接口百次执行时间:" +(end - start) +"ms");
}
}

三、 Test 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {

@Autowired
private UserService userService;

@Test
public void testFindUser(){
System.out.println(userService.findAll());
}

@Test
public void testSave(){
userService.save();
}

@Test
public void testUpdate(){
userService.update();
}
}

四、 AOP 中获取实例信息

  • 通过 pjp.getSignature 获取所运行实例的签名信息
  • signature.getDeclaringTypeName 获取包/类名路径
  • signature.getName 获取运行的方法名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
@Aspect
public class MyAdvice(){
@Pointcut("execution(* cn.pangcy.service.*Service.*())")
private void servicePt();

@Around("servicePt")
public void runSpeed(ProceedingJoinPoint pjp) throws Throwable {
// 获取执行签名信息
Signature signature = pjp.getSignature();
// 通过签名获取执行类型(接口名)
String declaringTypeName = signature.getDeclaringTypeName();
// 通过签名获取执行操作名称(方法名)
String name = signature.getName();
long start = System.currentTimeMillis();
for(int i = 0; i < 100; i++){
pjp.proceed();
}
long end = System.currentTimeMillis();
System.out.println("业务层接口百次执行时间:" + declaringTypeName +"." + name +" >>> " +(end - start) +"ms");
}
}

AOP 案例
https://blog.pangcy.cn/2023/04/11/后端编程相关/java/spring/AOP 案例/
作者
子洋
发布于
2023年4月11日
许可协议