springboot整合AOP和springmvc中的拦截器

依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

AOP通知类

package com.hlxy.mongodb.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAdvice {
    //<aop:pointcut expression="execution( * cn.zj.spring.service..*.*(..))" id="当前方法名就是id"/>
    //com.hlxy.mongodb.service包下的所以方法都会触发通知
    @Pointcut("execution(* com.hlxy.mongodb.service..*.*(..))")
    public void mongodbAdvice(){}

    @Before("mongodbAdvice()")
    public void begin(){
        System.out.println("前置通知");
    }
    @After("mongodbAdvice()")
    public void after(){
        System.out.println("后置通知");
    }

    @AfterReturning("mongodbAdvice()")
    public void AfterReturning(){
        System.out.println("最终通知。报异常,就不执行");
    }

    @AfterThrowing(pointcut = "mongodbAdvice()",throwing="ex")
    public void AfterThrowing(Throwable ex){
        System.out.println("报异常时执行"+ex.getMessage());
    }

    @Around("mongodbAdvice()") //<aop:around method="allInOne" pointcut-ref="pt"/>
    public Object allInOne(ProceedingJoinPoint pjp) {
        Object result = null;
        try {
            System.out.println("开启事务------");
            //执行被代理对象当前需要执行业务方法
            result = pjp.proceed();
            System.out.println("提交事务------");
        } catch (Throwable ex) {
            System.out.println("------回滚事务 : " + ex.getMessage());
        }finally {
            System.out.println("关闭session------");
        }
        return result;
    }

}

springmvc中的拦截器

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------------拦截器被访问了--------------------");
        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("userName");
        String password = (String) session.getAttribute("password");
        System.out.println(userName);
        System.out.println(password);
        if ("dc".equals(userName) && "123456".equals(password)){
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

配置类

@Configuration  //表明该类为一个配置类
public class MyConfig implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/login");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值