Spring-Boot:拦截器注解范例

package com.example.aop;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * Created by liz19 on 2017/1/30.
 */

@Target(ElementType.METHOD)
@Retention(RUNTIME)
@Documented
public @interface Action {

    String name();

}
package com.example.aop;

import org.springframework.stereotype.Service;

/**
 * Created by liz19 on 2017/1/30.
 */
@Service
public class DemoAnnotationService {

    @Action(name="注解式拦截的add操作")
    public void add(){
        System.out.println("clas DemoAnnotationService");
    }
}
package com.example.aop;

import org.springframework.stereotype.Service;

/**
 * Created by liz19 on 2017/1/30.
 */
@Service
public class DemoMethodService {

    public  void add(){

        System.out.println("class DemoMethodService");
    }
}
package com.example.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

/**
 * Created by liz19 on 2017/1/30.
 */

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.example.aop.Action)")
    public void annotationPointCut(){}

    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint){

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();

        Action action = method.getAnnotation(Action.class);

        System.out.println("注解式拦截--------Name: "+action.name());
    }

    @Before("execution(* com.example.aop.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint){

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();

        System.out.println("方法规则拦截拦截---------"+ method.getName());

    }

}
package com.example;

import com.example.aop.AopConfig;
import com.example.aop.DemoAnnotationService;
import com.example.aop.DemoMethodService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by liz19 on 2017/1/30.
 */
public class AopApp {

    public static void main(String[] args){

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);

        DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);

        demoAnnotationService.add();

        demoMethodService.add();

        context.close();
    }
}
package com.example.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Created by liz19 on 2017/1/30.
 */
@Configuration
@ComponentScan("com.example.aop")
@EnableAspectJAutoProxy
public class AopConfig {

}

1.通过@Aspect注解声明一个切面

2.通过@Component让此切面成为Spring容器管理的Bean

3.通过@PointCut注解声明切点

4.通过@After注解声明一个建言,并用@PointCut定义的切点

5.通过发射获得注解上的属性,然后做日志记录的相关操作,下面的相同

6.通过@Before注解声明一个建言,此建言直接使用拦截规则作为参数

7.通过@EnableAspectJAutoProxy开启对AspectJ支持

时间: 2024-08-06 06:50:33

Spring-Boot:拦截器注解范例的相关文章

Spring MVC拦截器+注解方式实现防止表单重复提交

原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 注,如果是集群的方式,则需要将token放入到缓存中即可. 注解Token代码:java源码  Java代码 复制代码 收藏代码 1[email protected](ElementType.METHOD) 2[email protected] (RetentionPolicy.RUNTIME) 3.public @interface T

spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案

最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2.0,Spring 5.0 以后WebMvcConfigurerAdapter会取消掉.以下介绍下大体的内容,希望对大家都有所帮助. 以下WebMvcConfigurerAdapter 比较常用的重写接口 /** 解决跨域问题 **/ public void addCorsMappings(CorsRegistry registry) ; /** 添加拦截器 **/ void addI

spring boot拦截器中获取request post请求中的参数

最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取到参数,post是不行的,后来想到了使用流的方式,调用request.getInputStream()获取流,然后从流中读取参数,如下代码所示: String body = ""; StringBuilder stringBuilder = new StringBuilder(); Buf

spring boot 拦截器

@SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) { //String[] arss= environment.getActiveProfiles(); SpringApplication.run(Application.class, args); } @Bean public HandlerIntercep

22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但是提供更精细的的控制能力:在request被响应之前.req

Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题

1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每一个请求:它的三个方法能分别作用于每个请求的不同生命周期,你可以根据自己的需要来加入相应的处理逻辑 原文地址:https://www.cnblogs.com/li150dan/p/12072023.html

spring boot 拦截器添加

@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new YourInterceptor()).addPathPatterns("/xx/**")//要拦截的请求 .excludePathPattern

Spring Boot 2.X(九):Spring MVC - 拦截器(Interceptor)

拦截器 1.简介 Spring MVC 中的拦截器(Interceptor)类似于 Servlet 开发中的过滤器 Filter,它主要用于拦截用户请求并作相应的处理,它也是 AOP 编程思想的体现,底层通过动态代理模式完成. 2.定义实现类 拦截器有两种实现方式: 1.实现 HandlerInterceptor 接口 2.继承 HandlerInterceptorAdapter 抽象类(看源码最底层也是通过 HandlerInterceptor 接口 实现) 3.HandlerIntercep

拦截器-注解

首先定义我们自己的Interceptor package com.web.interceptor; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.StrutsStatics; import org.apache.struts2.convention.annotation.ParentPackage; import com.common.utils.StringUtil; import com.ope