SSM[spring] 基于xml的AOP使用

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.25.RELEASE</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
[IAccountService]
1
2
3
4
5
6
7
8
9
package com.gphper;

public interface IAccountService {
void saveAccount();

void updateAccount(int i);

void deleteAccount();
}
[AccountServiceImp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.gphper.Imp;

import com.gphper.IAccountService;

public class AccountServiceImp implements IAccountService {
@Override
public void saveAccount() {
System.out.println("保存账号成功");
}

@Override
public void updateAccount(int i) {
System.out.println("更新账号id"+i);
}

@Override
public void deleteAccount() {
System.out.println("删除账号成功");
}
}
[LogUtil]
1
2
3
4
5
6
7
package com.gphper.utils;

public class LogUtil {
public void printLog(){
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="accountService" class="com.gphper.Imp.AccountServiceImp"></bean>

<bean id="log" class="com.gphper.utils.LogUtil"></bean>

<!--
表达式的写法
1.修饰可以不用写
2.可以使用 * 代替任意返回值
3.使用 *.. 表示任意包及其下的子包
4.使用 *.* 表示任意类的任意方法
5.使用 (..) 匹配任意数量的参数 (*) 匹配带有一个参数的方法
-->

<aop:config>
<aop:aspect id="logAop" ref="log">
<aop:before method="printLog" pointcut="execution(* *..*.*(..))"></aop:before>
</aop:aspect>
</aop:config>

<!-- 还可以这样写 -->
<aop:config>
<aop:aspect id="logAop" ref="log">
<aop:before method="printLog" pointcut-ref="pt1"></aop:before>
<!-- 避免多个切片做成重复写 expression-->
<!-- aop:pointcut 也可以定义在 aop:aspect外面-->
<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
[Test]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.test;

import com.gphper.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApoTest {

public static void main(String[] args) {

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

IAccountService as = (IAccountService)ac.getBean("accountService");

as.saveAccount();

}
}

环绕通知

[LogUtil]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.gphper.utils;

import org.aspectj.lang.ProceedingJoinPoint;

public class LogUtil {

public Object aroundNotify(ProceedingJoinPoint pjp){

Object valObj = null;
try {
Object[] args = pjp.getArgs();
System.out.println("通知前····");
valObj = pjp.proceed(args);
System.out.println("通知后····");
}catch (Throwable e){

}

return valObj;
}
}
1
2
3
4
5
6
7
8
<bean id="log" class="com.gphper.utils.LogUtil"></bean>
<aop:config>
<aop:aspect id="logAop" ref="log">
<aop:around method="aroundNotify" pointcut-ref="pt1"></aop:around>
<!-- 避免多个切片做成重复写 expression-->
<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
</aop:aspect>
</aop:config>