Skip to content

Commit 65d82d9

Browse files
author
Songyu
committed
修改文章内容
1 parent ac95ea5 commit 65d82d9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

_posts/2021-05-15-spring-aop记录访问日志.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public class LogAspect {
100100
systemLogDTO.setErrorDetail(JsonUtil.toJSONString(errorDetail));
101101
}
102102

103+
//此位置可以根据需求保存到数据库或文件中
103104
saveLog(systemLogDTO);
104105
}
105106
}
@@ -152,6 +153,36 @@ public class SystemLogDTO {
152153

153154
这里我使用了**@AfterReturn****@AfterThrowing**,为了区分记录正常返回和异常返回的日志。
154155

156+
## 设置切面启动开关
157+
158+
按照上述操作,设置好了定义好了切面类之后,还需要在入口或配置文件中进行设置开启。
159+
160+
#### spring boot
161+
在入口文件中增加@EnableAspectJAutoProxy(proxyTargetClass = true)注解
162+
{% highlight java %}
163+
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class,org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
164+
@EnableAspectJAutoProxy(proxyTargetClass = true)
165+
public class Application {
166+
167+
public static void main(String[] args) {
168+
169+
SpringApplication.run(Application.class, args);
170+
}
171+
}
172+
{% endhighlight %}
173+
174+
#### spring mvc
175+
springmvc需要在spring-mvc文件中增加配置进行开启
176+
{% highlight xml %}
177+
<aop:aspectj-autoproxy proxy-target-class="true"/>
178+
{% endhighlight %}
179+
180+
记得在命名空间中,加入对应的地址
181+
{% highlight xml %}
182+
http://www.springframework.org/schema/aop
183+
http://www.springframework.org/schema/aop/spring-aop.xsd
184+
{% endhighlight %}
185+
155186
## 小结
156187

157188
以往采用的方案是以拦截器的方式进行记录。需要配置拦截器以及放行的url,相对要繁琐一些。使用切面的话,可以以所有的控制器为切点,只需要一个切面类,就可以实现日志的访问记录,最重要的是对原来的逻辑没有任何入侵。

0 commit comments

Comments
 (0)