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. 个人永久性免费-Excel催化剂功能第91波-地图数据挖宝之行政区域信息实时下载(含经纬度)

    移动互联网和O2O兴起的这十年时间里,由地图LBS功能衍生出一大堆的极高商业价值的数据及应用,地图相关的数据,也是数据分析过程中一个大宝藏,从此篇开始将带给大家一系列的地图相关的数据采集,满足数据分析 ...

  2. Excel催化剂开源第2波-自动检测Excel的位数选择对应位数的xll文件安装

    Excel插件的部署问题难倒了不了的用户,特别是VSTO的部署,用ExcelDna开发的xll文件部署方便,不挑用户机器环境,是其开发Excel插件的一大优势. 其开发出来的xll文件,最终还是需要考 ...

  3. c语言进阶14-线性表之链表

    一.  线性表的链式存储结构 1.        顺序存储结构不足的解决办法 前面我们讲的线性表的顺序存储结构.它是有缺点的,最大的缺点就是插入和删除时需要移动大量元素,这显然就需要耗费时间.能不能想 ...

  4. 回顾二分与bfs(或者说是递推)和简单模拟

    今天,阳光正好,适合敲代码,诸事皆宜. 先来两道简单的模拟题. 第一道 机器翻译 输出为5. 代码思路:很明显需要用到队列来存单词,在建立一个bool数组来存储队列中有没有这个单词,需不需要向外界查询 ...

  5. 解决Oracle.DataAccess.Client.OracleConnection”的类型初始值设定项引发异常。

    解决Oracle.DataAccess.Client.OracleConnection”的类型初始值设定项引发异常. 这个问题他们说是oracle的版本问题 但是好像不是...(我感觉VS版本问题,我 ...

  6. 任何类型的数据都向String转型

    String从其定义上发现首字母大写,所以此为一个类,属于引用数据类型,但是此类属于系统的类. (1)String像普通变量一样直接通过复制的方式进行声明.字符串使用双引号括起来.两个字符串使用&qu ...

  7. 一文带你了解Java反射机制

    想要获取更多文章可以访问我的博客 - 代码无止境. 上周上班的时候解决一个需求,需要将一批数据导出到Excel.本来公司的中间件组已经封装好了使用POI生成Excel的工具方法,但是无奈产品的需求里面 ...

  8. some (1)

    每次在写博客的时候,都是自己觉得在工作中非常重要的东西,写东西的时候,也是一个思考的过程.好的东西不光帮助别人,也使自己有进一步的理解.

  9. Linux下安装配置Jmeter5.1,并执行jmx文件

    Windows下的jmeter是GUI模式,可查看操作,但是GUI对性能的干扰比较大,所有一般压测会在Linux上运行. 下面是Linux下安装配置Jmeter5.1,并执行jmx文件的步骤, 一.安 ...

  10. Android Studio "cannot resolve symbol R" 问题

    初接触Android Studio,又遇到了 "cannot resolve symbol R"问题(以前在 Eclipse 也遇到过),网上方法不一,后来在stackoverfl ...