SpringBoot实现简单AOP实例

一直很难理解Spring AOP思想的含义,于是决定实现一个实例来加深自己的理解。

本文全部转载于: https://blog.csdn.net/jdk_wangtaida/article/details/86639375

简单总结一下步骤:

  1. 新建springboot项目,其中pom.xml引入的依赖如下
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		######实现AOP的关键依赖
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		
		#####
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.7</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
  1. 定义基类,这里只要添加@Data注解就可以不用写getter, setter, toString等方法,如果@Data添加没有生效,可能是插件没有安装。最好在创建项目的时候就选好Lombok支持。
package com.example.springboot_aop;

import lombok.Data;

/**
 * @author shmilyiselephant
 * @date 25.03.20
 * @decription
 */

@Data
public class User {
    private String name;
    private int age;
}
  1. 实现切面类
package com.example.springboot_aop;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
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.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;


@Aspect//这个注解的作用是:将一个类定义为一个切面类
@Component//这个注解的作用:把切面类加入到IOC容器中
@Order(1)//这个注解的作用是:标记切面类的处理优先级,i值越小,优先级别越高.PS:可以注解类,也能注解到方法上
@Slf4j
public class AspectDemo {
    private Gson gson = new Gson();
 
    //申明一个切点 里面是excution表达式
    @Pointcut("execution(* com.dada.springboot_aop..*.*(..))")
    private void webLog() {
    }
 
    //请求method前打印内容
    @Before(value = "webLog()")//这个注解的作用是:在切点前执行方法,内容为指定的切点
    public void methodBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //打印请求内容
        log.info("========================请求内容======================");
        log.info("请求地址:" + request.getRequestURI().toString());
        log.info("请求方式" + request.getMethod());
        log.info("请求类方法" + joinPoint.getSignature());
        log.info("请求类方法参数" + Arrays.toString(joinPoint.getArgs()));
        log.info("========================请求内容======================");
 
    }
 
    //在方法执行完结后打印返回内容
    @AfterReturning(returning = "o", pointcut = "webLog()")
    //这个注解的作用是:在切入点,return后执行,如果想对某些方法的返回参数进行处理,可以在这操作
    public void methodAfterReturing(Object o) {
        log.info("--------------返回内容----------------");
        log.info("Response内容:" + gson.toJson(o));
        log.info("--------------返回内容----------------");
 
    }
 
}

  1. 实现一个简单的controller
package com.example.springboot_aop.controller;

import com.example.springboot_aop.User;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author shmilyiselephant
 * @date 25.03.20
 * @decription
 */
@Controller
@RequestMapping("/test")
public class IndexController {

    @RequestMapping(value = "/testAspect", method = RequestMethod.GET)
    public @ResponseBody User test(@RequestParam("name") String name) {
        User user = new User();
        user.setName("a");
        user.setAge(12);
        return user;
    }
}

  1. 访问 http://localhost:8080/test/testAspect?name=www即可在console看见结果
2020-03-25 18:04:58.690  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : ======request info======
2020-03-25 18:04:58.691  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : url: /test/testAspect
2020-03-25 18:04:58.691  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : method: GET
2020-03-25 18:04:58.691  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : params: [xxxx]
2020-03-25 18:04:58.691  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : ======request info======
2020-03-25 18:04:58.705  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : ------return info------
2020-03-25 18:04:58.710  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : response: {"name":"a","age":12}
2020-03-25 18:04:58.710  INFO 9420 --- [nio-8080-exec-2] com.example.springboot_aop.AspectDemo    : ------return info------

先打印@Before中的参数,之后打印@AfterReturning的结果

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值