spring boot学习(8) SpringBoot 之切面AOP
@Aspect注解是切面注解类
@Pointcut切点定义
@Before是方法执行前调用
@After是方法执行后调用
@AfterReturning方法执行返回值调用
Service层本身就可以切入事务,所以我们这类搞个常用的 切controller层方法
每个执行controller层的方法 都记录下请求Url,访问者IP 执行类方法参数等信息;
紧接上一讲,这里只是多了切面类,项目结构:
贴下代码:
1.切面类com.cy.aspect.RequestAspect.java:
package com.cy.aspect; import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.cy.entity.Student;
import ch.qos.logback.classic.Logger; /**
* 切面类
*/
@Aspect
@Component
public class RequestAspect { private Logger logger= (Logger) LoggerFactory.getLogger(RequestAspect.class); /**
* Pointcut定义切点
* public修饰符的 返回值任意 com.cy.controller包下面的任意类的任意方法任意参数
*/
@Pointcut("execution(public * com.cy.controller.*.*(..))")
public void log(){ } @Before("log()")
public void doBefore(JoinPoint joinPoint){
logger.info("方法执行前...");
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
logger.info("url: " + request.getRequestURI()); //url
logger.info("ip: " + request.getRemoteHost()); //ip
logger.info("method: "+request.getMethod()); //post or get? or ?
logger.info("class.method: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("args: "+joinPoint.getArgs());
Student student = (Student) joinPoint.getArgs()[0];
logger.info(student.toString());
} @After("log()")
public void doAfter(JoinPoint joinPoint){
logger.info("方法执行后...");
} @AfterReturning(returning="result", pointcut="log()")
public void doAfterReturnint(Object result){
logger.info("方法返回值:" + result);
}
}
回顾前一章的StudentController是这样的:
/**
* 添加学生
* @param student
* @return
*/
@RequestMapping("/add")
public String add(@Valid Student student, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return bindingResult.getFieldError().getDefaultMessage();
}else{
studentService.add(student);
return "添加成功";
}
}
测试:
http://localhost/studentAdd.html正常添加一个学生,查看控制台信息:
2018-03-25 16:34:53.984 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : 方法执行前...
2018-03-25 16:34:53.984 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : url: /student/add
2018-03-25 16:34:53.984 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : ip: 0:0:0:0:0:0:0:1
2018-03-25 16:34:53.984 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : method: POST
2018-03-25 16:34:53.986 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : class.method: com.cy.controller.StudentController.add
2018-03-25 16:34:53.986 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : args: [Ljava.lang.Object;@4232f676
2018-03-25 16:34:53.986 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : Student [id=null, name=余学海, age=28]
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into t_student (age, name, id) values (?, ?, ?)
2018-03-25 16:34:54.067 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : 方法执行后...
2018-03-25 16:34:54.067 INFO 3744 --- [p-nio-80-exec-1] com.cy.aspect.RequestAspect : 方法返回值:添加成功
spring boot学习(8) SpringBoot 之切面AOP的更多相关文章
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- spring boot 学习(十)SpringBoot配置发送Email
SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...
- spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)
第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...
- spring boot学习(2) SpringBoot 项目属性配置
第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...
- spring boot学习(3) SpringBoot 之MVC 支持
第一节:@RequestMapping 配置url 映射 第二节:@Controller 处理http 请求 转发到一个页面,以前是转发到jsp页面,现在使用freemarker: 在pom.xm ...
- spring boot学习(7) SpringBoot 之表单验证
第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能: 下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁. 贴下代码吧: Student实体: ...
- spring boot学习(6) SpringBoot 之事务管理
两个操作要么同时成功,要么同时失败: 事务的一致性: 以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来 ...
- Spring boot 学习八 Springboot的filter
一: 传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...
随机推荐
- 相对和绝对路径 mkdir cd rm 等命令
1. 绝对路径和相对路径 个人理解: 绝对路径-----即从根目录开始一直到你需要找的文件或目录的路径 (即任何情况下都以根目录为起点) 相对路径------即从当前目录开始一直找到你需要找的 ...
- hdu 1280 堆排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- java依赖注入(injection)
和SpringSource分别通过其开源项目Guice及Spring Framework提供了依赖注入的功能.然而直到现在开发者也没有一种标准的.独立于供应商的方式从而无需修改其源文件就能在这些框架之 ...
- 将本地项目上传到git/码云
idea查看任意项目的远程仓库地址: git remote -v git branch -v git branch -d 分支名 删除本地分支 git branch -D 分支名 ...
- JS字符串和正则总结
trim功能:去除字符串开始和结尾的空格. 中间空格不去掉~ 对输入字符串的处理,多输要先清除开头结尾空格,再处理 IE8不支持trim()方法. String总结:所有API都无法修改原字符串,都会 ...
- 【java多线程】线程状态分析
一.java线程的状态 NEW: 新建状态,线程对象已经创建,但尚未启动 RUNNABLE:就绪状态,可运行状态,调用了线程的start方法,已经在java虚拟机中执行,等待获取操作系统资源如CPU, ...
- golang sublime text3 自动补全
按下快捷键 command+ shift +p 调出控制台 输入install 然后输入Golang Tools Integration 安装Golang Tools Integration 插件即 ...
- ClusterControl 强大免费数据管理工具
几张参考图 galera 集群管理 多种数据库管理 组件架构 参考资料 https://severalnines.com/docs/intro.html
- ONVIF让NVR与网络监控摄像机更"亲密"
NVR的发展目前看主要分为二大类,一类是接入级的嵌入式NVR,其主要针对主流的IP摄像机研发的一种NVR. 另一类是针对社区和平安城市级的大型NVR,其主要采用的是以高端服务器软.硬件结构为基础,以传 ...
- cos migration工具webhook推送
上一篇讲了腾讯云同步工具的使用,这篇主要是补充如何将同步结果主动消息通知. 因为cos migration 工具是java语言,并在github开源的,所以可以直接修改源码,添加webhook推送代码 ...