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支持

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

  1. Spring boot拦截器的实现

    Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...

  2. 【spring boot】spring boot 拦截器

    今日份代码: 1.定义拦截器 import com.alibaba.fastjson.JSON; import org.apache.commons.collections.CollectionUti ...

  3. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

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

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

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

    Springboot中静态资源和拦截器处理(踩了坑)   背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...

  6. spring boot拦截器

    实现自定义拦截器只需要3步: 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addI ...

  7. Spring boot 拦截器和过滤器

    1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...

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

    原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 注,如果是集群的方式,则需要将token ...

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

    最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...

  10. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)

    文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...

随机推荐

  1. Linux中的保护机制

    Linux中的保护机制 在编写漏洞利用代码的时候,需要特别注意目标进程是否开启了NX.PIE等机制,例如存在NX的话就不能直接执行栈上的数据,存在PIE 的话各个系统调用的地址就是随机化的. 一:ca ...

  2. [剑指offer] 6. 旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  3. Linux系统安装jdk——.tar.gz版

    1.rpm.deb.tar.gz的区别: rpm格式的软件包适用于基于Red Hat发行版的系统,例如Red Hat Linux.SUSE.Fedora. deb格式的软件包则是适用于基于Debian ...

  4. [Revit]Autodesk Revit 二次开发整理(资料、准备工作和环境搭建)

    1 前言 Revit被Autodesk收购之后,整理和开放了一大部分API,供开发者实现自己的功能和程序,总体来说API的功能比较完善,毕竟市面上已经出现了各式各样的插件. 本人也是初学者,在Revi ...

  5. 关于 64位系统 java连接access 报错java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    报错的原因是url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=E:/公司/2000.mdb"; 这样是不行 ...

  6. Oracle JDK与OpenJDK到底有什么不同?

    ​不知道各位developer平时是否有过疑问,Oracle JDK是什么,OpenJDK又是什么? Oracle JDK便是平常我们在windows系统上做开发使用的JDK,又称作SUN JDK.O ...

  7. 02-Kubenetes资源

    目录 Kubenetes资源 常用资源对象 标签labels 创建资源的方式 Pod pods.spec.containers 必须 nodeSelector <map [string]stri ...

  8. 04-kubernetes网络通信

    目录 kubernetes网络通信 需要解决的问题 flannel Calico/Cannel kubernetes网络通信 需要解决的问题 同一个pod内部的不同容器间通信, local Pod间的 ...

  9. Redis的HelloWorld

    1.安装完成的Redis: linux安装的应用默认会在:usr/local/bin. 1.redis-benchmark:性能测试工具,是redis提供的一个高并发程序,可以在自己本机运行,看看自己 ...

  10. 华为matebook14vm虚拟机错误

    1.创建时显示不支持64位虚拟机 测试环境: 华为matebook14 window10 专业工作站版  1903   问题描述: 创建虚拟机时显示:此主机不支持64位解决方案   问题参考: 参考1 ...