Skip to content

Commit c074b7f

Browse files
committed
xml 切面
1 parent 2893d60 commit c074b7f

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.luojia.xmlaop;
2+
3+
public interface Calculator {
4+
int add(int i, int j);
5+
6+
int sub(int i, int j);
7+
8+
int mul(int i, int j);
9+
10+
int div(int i, int j);
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.luojia.xmlaop;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
/**
6+
* 最基本的实现类
7+
*/
8+
@Component
9+
public class CalculatorImpl implements Calculator {
10+
@Override
11+
public int add(int i, int j) {
12+
int result = i + j;
13+
System.out.println("方法内部 result = " + result);
14+
// 模拟异常
15+
// int a = 1/0;
16+
return result;
17+
}
18+
19+
@Override
20+
public int sub(int i, int j) {
21+
int result = i - j;
22+
System.out.println("方法内部 result = " + result);
23+
return result;
24+
}
25+
26+
@Override
27+
public int mul(int i, int j) {
28+
int result = i * j;
29+
System.out.println("方法内部 result = " + result);
30+
return result;
31+
}
32+
33+
@Override
34+
public int div(int i, int j) {
35+
int result = i / j;
36+
System.out.println("方法内部 result = " + result);
37+
return result;
38+
}
39+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.luojia.xmlaop;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.*;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Arrays;
9+
10+
// @Component 注解保证这个切面类能够放入IOC容器
11+
@Component
12+
public class LogAspect {
13+
14+
/**
15+
* 前置通知
16+
*/
17+
public void beforeMethod() {
18+
System.out.println("LogAspect --> 前置通知");
19+
}
20+
21+
/**
22+
* 后置通知
23+
*/
24+
public void afterMethod(JoinPoint joinPoint) {
25+
String methodName = joinPoint.getSignature().getName();
26+
Object[] args = joinPoint.getArgs();
27+
System.out.println("LogAspect --> 后置通知,方法名称:" + methodName + ", 参数:" + Arrays.toString(args));
28+
}
29+
30+
/**
31+
*返回通知
32+
*/
33+
public void afterReturningMethod(JoinPoint joinPoint, Object result) {
34+
String methodName = joinPoint.getSignature().getName();
35+
System.out.println("LogAspect --> 返回通知,方法名称:" + methodName + ", 目标方法返回结果:" + result);
36+
}
37+
38+
/**
39+
* 异常通知
40+
*/
41+
@AfterThrowing(value = "execution(* com.luojia.xmlaop.CalculatorImpl.*(..))", throwing = "ex")
42+
public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex) {
43+
String methodName = joinPoint.getSignature().getName();
44+
System.out.println("LogAspect --> 异常通知,方法名称:" + methodName + ", 目标方法返回异常结果:" + ex);
45+
}
46+
47+
/**
48+
* 环绕通知
49+
*/
50+
public Object aroundMethod(ProceedingJoinPoint joinPoint) {
51+
String methodName = joinPoint.getSignature().getName();
52+
Object[] args = joinPoint.getArgs();
53+
Object result = null;
54+
try {
55+
System.out.println("LogAspect --> 环绕通知,目标方法之前执行,方法名:" + methodName + ", 参数:" + Arrays.toString(args));
56+
result = joinPoint.proceed();
57+
58+
System.out.println("环绕通知,目标方法返回值之后执行");
59+
} catch (Throwable e) {
60+
e.printStackTrace();
61+
System.out.println("环绕通知,目标方法出现异常执行");
62+
} finally {
63+
System.out.println("环绕通知,目标方法执行完毕后执行");
64+
}
65+
return result;
66+
}
67+
68+
// 重用切入点表达式
69+
@Pointcut(value = "execution(* com.luojia.xmlaop.CalculatorImpl.*(..))")
70+
public void pointCut() {
71+
72+
}
73+
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.luojia.xmlaop;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class TestAop {
8+
9+
@Test
10+
public void testAdd() {
11+
ApplicationContext context =
12+
new ClassPathXmlApplicationContext("beanxmlaop.xml");
13+
14+
Calculator calculator = context.getBean(Calculator.class);
15+
calculator.add(8, 9);
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:aop="http://www.springframework.org/schema/aop"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context.xsd
10+
http://www.springframework.org/schema/aop
11+
http://www.springframework.org/schema/aop/spring-aop.xsd">
12+
<!--
13+
基于注解的AOP的实现:
14+
1、将目标对象和切面交给IOC容器管理(注解+扫描)
15+
2、开启AspectJ的自动代理,为目标对象自动生成代理
16+
3、将切面类通过注解@Aspect标识
17+
-->
18+
<context:component-scan base-package="com.luojia.xmlaop"></context:component-scan>
19+
20+
<!-- 配置aop的物种通知类型 -->
21+
<aop:config>
22+
<!-- 配置切面类 -->
23+
<aop:aspect ref="logAspect">
24+
<!-- 配置切入点 -->
25+
<aop:pointcut id="pointcut" expression="execution(* com.luojia.xmlaop.CalculatorImpl.*(..))"/>
26+
<!-- 配置五种通知类型 -->
27+
<!--前置通知-->
28+
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
29+
30+
<!--后置通知-->
31+
<aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after>
32+
33+
<!--返回通知-->
34+
<aop:after-returning method="afterReturningMethod" returning="result" pointcut-ref="pointcut"></aop:after-returning>
35+
36+
<!--异常通知-->
37+
<aop:after-throwing method="afterThrowingMethod" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
38+
39+
<!--环绕通知-->
40+
<aop:around method="aroundMethod" pointcut-ref="pointcut"></aop:around>
41+
42+
</aop:aspect>
43+
</aop:config>
44+
45+
</beans>
46+

0 commit comments

Comments
 (0)