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催化剂功能第66波-数据快速录入,预定义引用数据逐字提示

    在前面好几波的功能中,为数据录入的规范性做了很大的改进,数据录入乃是数据应用之根,没有完整.干净的数据源,再往下游的所有数据应用场景都是空话.在目前IT化进程推进了20多年的现状,是否还仍有必要在Ex ...

  2. java多线程调用run和不调用run的区别

    当在java程序中创建一个线程的时候,会三种情况: 1,只运行run方法 2,先执行start,再执行run方法 3,只运行start方法 三者的区别如下: 运行方式 区别 只运行run 只运行run ...

  3. 数据库---T-SQL语句:查询语句(二)

    >查询: 一.查询所有数据: select * from Info    ---查询所有数据(行) select Name from Info  ---查询特定列(Name列) select N ...

  4. 2019牛客暑期多校训练营(第四场)K.number

    >传送门< 题意:给你一个字符串s,求出其中能整除300的子串个数(子串要求是连续的,允许前面有0) 思路: >动态规划 记f[i][j]为右端点满足mod 300 = j的子串个数 ...

  5. 【Java】Class JavaLaunchHelper is implemented in both ** and **

    详细问题描述如下: objc[64179]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachin ...

  6. 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk

    一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...

  7. java基础学习_io流之FileInputStream

    一.FileInputStream属性: /* File Descriptor - handle to the open file */private final FileDescriptor fd; ...

  8. javascript+jQuery补充

    一.jQuery事件绑定 <div class='c1'> <div> <div class='title'>菜单一</div> <div cla ...

  9. spark shuffle写操作三部曲之BypassMergeSortShuffleWriter

    前言 再上一篇文章 spark shuffle的写操作之准备工作 中,主要介绍了 spark shuffle的准备工作,本篇文章主要介绍spark shuffle使用BypassMergeSortSh ...

  10. 夯实Java基础(二)——面向对象之封装

    1.封装介绍 封装封装,见名知意,就是把东西包装隐藏起来,不被外界所看见, 而Java特性封装:是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数 ...