多语言展示
当前在线:1314今日阅读:168今日分享:49

如何在springboot中配置aop拦截器

如何在springboot中配置aop拦截器
工具/原料
1

springboot

2

intellij idea

方法/步骤
1

在spring里面还提供有一种aop拦截器配置,不过大部分的aop拦截器都是围绕着业务层进行拦截处理的。1、建立一个普通的业务操作接口和它的子类:package com.gwolf.service.impl;import com.gwolf.service.IMemberService;import com.gwolf.vo.Member;import org.springframework.stereotype.Service;@Servicepublic class MemberServiceImpl implements IMemberService{    @Override    public Member get(String mid) {        Member vo = new Member();        vo.setMid(mid);        vo.setName('张三');        return vo;    }}

2

现在业务层的操作完成之后随后去修改控制层,让控制层进行业务层的调用。package com.gwolf.controller;import com.gwolf.service.IMemberService;import com.gwolf.util.controller.AbstractBaseController;import com.gwolf.vo.Member;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Service;import org.springframework.validation.BindingResult;import org.springframework.validation.ObjectError;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.validation.Valid;import java.util.Iterator;@Controllerpublic class MemberController extends AbstractBaseController{    @Autowired    private IMemberService memberService;        @RequestMapping(value='/member_get', method = RequestMethod.GET)    @ResponseBody    public Object get(String mid) {        return this.memberService.get(mid);    }}

3

访问地址:http://localhost:8080/member_get?mid=100

4

现在的业务层只是一个纯粹的调用而已,但是现在希望对调用的过程进行拦截处理,所以要想实现这样的处理,那么就需要引入新的开发依赖包,修改pom.xml配置文件:            SpringBoot        com.gwolf        1.0-SNAPSHOT        4.0.0    com.gwolf    springboot-base    jar    springboot-base    http://maven.apache.org            UTF-8                            org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-test            test                            org.springframework            springloaded                            org.springframework.boot            spring-boot-devtools                            org.springframework.boot            spring-boot-starter-thymeleaf                            org.springframework.boot            spring-boot-starter-aop                            junit            junit            test           

5

编写一个aop拦截的控制程序类。package com.gwolf.config;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import java.util.Arrays;@Aspect@Componentpublic class ServiceAspect {    private Logger log = LoggerFactory.getLogger(ServiceAspect.class);        @Around('execution(* com.gwolf..service..*.*(..))')    public Object aroundInvoke(ProceedingJoinPoint point)             throws Throwable {        this.log.info('[***Service-Before***]执行参数:'                 + Arrays.toString(point.getArgs()));                Object object = point.proceed(point.getArgs());        this.log.info('[***Service-After***]返回结果:'                + object);        return object;    }}

6

查看我们业务层的aop拦截输出打印。

推荐信息