Spring Boot笔记九:AOP面向切面编程
我参考的这篇文章,以验证身份为例讲解了什么是AOP AOP
这里只讲解一下怎么去实现AOP
新建一个类,叫HttpAspect用来切面
package com.vae.springboot.study.aspect;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
@Component
public class HttpAspect {
//日志
Logger logger=LoggerFactory.getLogger(getClass());
private RuntimeNode attributes;
@Pointcut("execution(public * com.vae.springboot.study.Controller.HelloController.*(..))")
public void log(){ }
@Before("log()")
public void doBefore(){
ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=attributes.getRequest();
logger.info("假装我在这里验证了用户身份");
//url
logger.info("url={}",request.getRequestURI());
//方法
logger.info("method={}",request.getMethod());
//ip
logger.info("ip={}",request.getRemoteAddr());
//类方法
//logger.info("class_method={}",joinPoint.getSignature());
//参数
//logger.info("arg={}",joinPoint.getArgs());
}
@After("log()")
public void doAfter(){
logger.info("假装我在这里处理最后的事情");
}
}
这里面需要讲的东西就是@Aspect注解
@Aspect
作用是把当前类标识为一个切面供容器读取
@Before
标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有
@AfterReturning
后置增强,相当于AfterReturningAdvice,方法正常退出时执行
@AfterThrowing
异常抛出增强,相当于ThrowsAdvice
@After
final增强,不管是抛出异常或者正常退出都会执行
@Around
环绕增强,相当于MethodInterceptor
@DeclareParents
引介增强,相当于IntroductionInterceptor
看一下我们的Controller
package com.vae.springboot.study.Controller;
import com.vae.springboot.study.bean.Person;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.util.Map;
@Controller
public class HelloController {
@PostMapping("/test")
public String test(@Valid Person person, BindingResult bindingResult){
System.out.println("test方法");
if (bindingResult.hasErrors()) {
System.out.println(bindingResult.getFieldError().getDefaultMessage());
return null;
}
person.setName(person.getName());
person.setAge(person.getAge());
return "Vae";
}
}
运行,然后使用PostMan执行一下http://localhost:8080/test

Spring Boot笔记九:AOP面向切面编程的更多相关文章
- Spring之注解实现aop(面向切面编程)
1:Aop(aspect object programming)面向切面编程,名词解释: 1.1:功能:让关注点代码与业务逻辑代码分离 1.2:关注点 重复代码就叫做关注点 ...
- 不依赖Spring使用AspectJ达到AOP面向切面编程
网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- 学习笔记: AOP面向切面编程和C#多种实现
AOP:面向切面编程 编程思想 OOP:一切皆对象,对象交互组成功能,功能叠加组成模块,模块叠加组成系统 类--砖头 系统--房子 类--细胞 系统--人 ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
随机推荐
- 在linux下安装并配置mysql数据库
在linux下安装并配置mysql数据库 工具/原料 MySql5.6 CentOS 方法/步骤 1 查找以前是否安装有mysql,使用下面命令: rpm -qa|grep -i mysql ...
- Dividing POJ - 1014 多重背包二进制优化
多重背包模型 写的时候漏了一个等号找了半天 i<<=1 !!!!!! #include<iostream> #include<cstdio> #include&l ...
- 洛谷P1373小a和uim大逃离题解
题目 这个题好坑啊,首先是他会卡空间,然后我们就只能把一种比较好理解的状态给舍弃,因为空间开不下,然而采用一种难理解的状态就是\(dp[i][j][l][0/1]\)表示\(i\),\(j\)位置,两 ...
- jsp小测试--猜大小
page1.jpg: <%@ page language="java" import="java.util.*" pageEncoding="g ...
- HBase 清空表数据
public int clearTableByTableName(String tableName) throws Exception { logger.debug("======InitH ...
- 不可解问题之停机问题(Undecidable Problem Halting Problem)
计算机技术已运用到人类生活的方方面面,帮助人类解决各种问题.可你是否有想过,计算机是否能为人类解决所有问题呢? 假如你是一个程序猿,你已编写过很多程序.有些程序一下子就能出结果,有些程序则好久都没有显 ...
- Power Stations HDU - 3663
我为什么T了.... Power Stations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Win10 GodMode
Win10 GodMode 文件夹命名示例: GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} {ED7BA470-8E54-465E-825C-99712 ...
- 「HDU - 2857」Mirror and Light(点关于直线的对称点)
题目链接 Mirror and Light 题意 一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点.(都在一个二维平面内) 题解 找出入射光线关于镜子直线的对称点,然后和反射光线连边 ...
- Hdoj 1312.Red and Black 题解
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...