Skip to content

Commit 53e06b6

Browse files
committed
test controller aspect
1 parent a19d12a commit 53e06b6

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cn.com.ttblog.ssmbootstrap_table.aop;
2+
3+
import org.apache.commons.lang3.builder.ToStringBuilder;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.aspectj.lang.annotation.After;
7+
import org.aspectj.lang.annotation.AfterReturning;
8+
import org.aspectj.lang.annotation.AfterThrowing;
9+
import org.aspectj.lang.annotation.Around;
10+
import org.aspectj.lang.annotation.Aspect;
11+
import org.aspectj.lang.annotation.Before;
12+
import org.aspectj.lang.annotation.Pointcut;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
import org.springframework.stereotype.Component;
16+
17+
@Component
18+
@Aspect
19+
public class ControllerAspect {
20+
21+
private static final Logger LOG = LoggerFactory
22+
.getLogger(ControllerAspect.class);
23+
24+
@Pointcut("execution(* cn.com.ttblog.ssmbootstrap_table.controller..*.*(..))")
25+
public void pointCut() {
26+
}
27+
28+
@After("pointCut()")
29+
public void after(JoinPoint joinPoint) {
30+
}
31+
32+
@Before("pointCut()")
33+
public void before(JoinPoint joinPoint) {
34+
// Object[] args = joinPoint.getArgs();
35+
LOG.warn("控制器切面参数 param:{}",
36+
ToStringBuilder.reflectionToString(joinPoint));
37+
Object[] arr = joinPoint.getArgs();
38+
if (arr.length > 0) {
39+
LOG.warn("method param:{}",
40+
ToStringBuilder.reflectionToString(joinPoint.getArgs()[0]));
41+
}
42+
}
43+
44+
@AfterReturning(pointcut = "pointCut()", returning = "returnVal")
45+
public void afterReturning(JoinPoint joinPoint, Object returnVal) {
46+
}
47+
48+
@Around("pointCut()")
49+
public Object around(ProceedingJoinPoint pjp) throws Throwable {
50+
Object obj = null;
51+
long startTime = System.nanoTime();
52+
long endTime;
53+
try {
54+
obj = pjp.proceed();
55+
endTime = System.nanoTime();
56+
} catch (Throwable ex) {
57+
throw ex;
58+
}
59+
LOG.warn(pjp.getSignature() + "-time consume is "
60+
+ (endTime - startTime) / 1000 / 1000 / 1000 * 0.1 + "s");
61+
return obj;
62+
}
63+
64+
@AfterThrowing(pointcut = "pointCut()", throwing = "error")
65+
public void afterThrowing(JoinPoint jp, Throwable error) {
66+
LOG.error("控制器发生错误:" + error);
67+
}
68+
}

src/main/resources/spring/spring-context.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
66
xmlns:tx="http://www.springframework.org/schema/tx"
77
xsi:schemaLocation="http://www.springframework.org/schema/beans
8-
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
8+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
99
http://www.springframework.org/schema/context
10-
http://www.springframework.org/schema/context/spring-context-3.2.xsd
10+
http://www.springframework.org/schema/context/spring-context-4.0.xsd
1111
http://www.springframework.org/schema/mvc
12-
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
13-
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
14-
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
12+
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
13+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
14+
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
1515

1616
<context:component-scan base-package="cn.com.ttblog.ssmbootstrap_table">
1717
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
@@ -35,7 +35,7 @@
3535
</bean>
3636

3737
<!-- 启用aop -->
38-
<aop:aspectj-autoproxy />
38+
<aop:aspectj-autoproxy/>
3939

4040
<!-- 加载资源文件 -->
4141
<import resource="spring-mybatis.xml" />

src/main/resources/spring/spring-mvc.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
44
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:aop="http://www.springframework.org/schema/aop"
56
xmlns:mvc="http://www.springframework.org/schema/mvc"
67
xsi:schemaLocation="http://www.springframework.org/schema/beans
78
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
89
http://www.springframework.org/schema/context
9-
http://www.springframework.org/schema/context/spring-context-4.0.xsd
10+
http://www.springframework.org/schema/context/spring-context-4.0.xsd
11+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
1012
http://www.springframework.org/schema/mvc
1113
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
1214
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
@@ -193,6 +195,8 @@
193195
<property name="paramName" value="lang"/>
194196
</bean>
195197

198+
<aop:aspectj-autoproxy proxy-target-class="true"/>
199+
196200
<mvc:interceptors>
197201
<!-- 国际化拦截器 -->
198202
<!-- <ref bean="localeChangeInterceptor"/> -->

0 commit comments

Comments
 (0)