File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ public class LogAspect {
100
100
systemLogDTO.setErrorDetail(JsonUtil.toJSONString(errorDetail));
101
101
}
102
102
103
+ //此位置可以根据需求保存到数据库或文件中
103
104
saveLog(systemLogDTO);
104
105
}
105
106
}
@@ -152,6 +153,36 @@ public class SystemLogDTO {
152
153
153
154
这里我使用了** @AfterReturn ** 和** @AfterThrowing ** ,为了区分记录正常返回和异常返回的日志。
154
155
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
+
155
186
## 小结
156
187
157
188
以往采用的方案是以拦截器的方式进行记录。需要配置拦截器以及放行的url,相对要繁琐一些。使用切面的话,可以以所有的控制器为切点,只需要一个切面类,就可以实现日志的访问记录,最重要的是对原来的逻辑没有任何入侵。
You can’t perform that action at this time.
0 commit comments