springboot-3-aop
aop存在的目的是进一步解耦, spring支持aspectJ的注解式切面编程
1), 使用@Aspect声明为一个切面, 并使用@Component加入context中
2), 使用@After, @Before, @Aroud定义advice, 可直接引入 pointcut
代码实现:
1, 引入aspectJ的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.</version>
</dependency>
2, 自定义注解
package com.wenbronk.aop; import org.springframework.stereotype.Component; /**
* Created by wenbronk on 2017/5/13.
*/
@Component
public class InterceptAnnotation { @AopAnnotation(value = "注解拦截")
public void intercept() {
System.out.println("annotation intercepting");
} }
3, 自定义方法拦截
package com.wenbronk.aop; import org.springframework.stereotype.Component; /**
* 方法拦截
* Created by wenbronk on 2017/5/13.
*/
@Component
public class IntercepterMethod {
public void intercepter() {
System.out.println("method intercepter");
}
}
4, 编写切点
package com.wenbronk.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 wenbronk on 2017/5/13.
*/
@Aspect
@Component
public class AopAspect { @Pointcut(value = "@annotation(com.wenbronk.aop.AopAnnotation)") // 切点为注解
public void pointCut() {}; /**
* 注解式拦截, 可在@After, @Before, @Aroud中直接引入 @pointCut
* @param joinPoint
*/
@After(value = "pointCut()")
public void afterPointCut(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
AopAnnotation annotation = method.getAnnotation(AopAnnotation.class);
System.out.println("注解式拦截: " + annotation.value());
} /**
* 方法式拦截
* @param joinPoint
*/
@Before(value = "execution(* com.wenbronk.aop.IntercepterMethod.*(..))")
public void beforePointCut(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法式拦截: " + method.getName());
} }
5, javaconfig
如果你使用的是springboot方式, 那么需要在启动类上添加 @EnableAspectJAutoProxy 开启AspectJ的支持
package com.wenbronk.aop; import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy; /**
* 配置类, 省却Application.java
* Created by wenbronk on 2017/5/13.
*/
@SpringBootConfiguration
@ComponentScan(basePackages = {"com.wenbronk.aop"})
@EnableAspectJAutoProxy // 开启对AspectJ的支持
public class AopConfig { }
6, 测试
package com.wenbronk.aop; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* 测试类
* Created by wenbronk on 2017/5/13.
*/
public class TestAop { public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); // 注解式拦截
InterceptAnnotation annotation = context.getBean(InterceptAnnotation.class);
annotation.intercept(); // 方法拦截
IntercepterMethod method = context.getBean(IntercepterMethod.class);
method.intercepter(); context.close(); } }
http://www.cnblogs.com/wenbronk/
springboot-3-aop的更多相关文章
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot切面Aop的demo简单讲解
前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解. SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 切面(Aop) 一.概 ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.
对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...
- (办公)springboot配置aop处理请求.
最近项目用到springboot,就是需要配置一些东西.比如用aop处理请求.方法前通知获取url,method,ip,类方法,参数,方法后通知,返回参数,而且还可以记录一下日志.下面是操作的代码. ...
- SpringBoot系列——aop 面向切面
前言 项目中我们经常会用到aop切面,比如日志记录:这里简单记录一下springboot是如何使用aop spring对aop的配置,来自springboot参考手册,Common applicati ...
- SpringBoot使用AOP
本文介绍SpringBoot中使用Spring AOP. 简介 AOP简介 AOP可能对于广大开发者耳熟能详,它是Aspect Oriented Programming的缩写,翻译成中文就是:面向切面 ...
- Spring全家桶系列–SpringBoot之AOP详解
//本文作者:cuifuan //本文将收录到菜单栏:<Spring全家桶>专栏中 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关 ...
- SpringBoot配置Aop笔记【例子】
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
随机推荐
- [翻译]Event Handler Description 事件处理描述
Event Handler Description 事件处理描述 (自定义控件) How should a new event handler be defined if it does not ...
- solr-4.10.2版本使用tomcat7部署
当前版本仅限于solr-4.10.2版本.默认环境使用的是jdk1.7,tomcat7.环境自己配置.网上一堆堆的. 1.下载相应的文件(solr-4.10.2.zip). 官网地址:http://l ...
- Hadoop map任务数量的计算
Hadoop中决定map个数的的因素有几个,由于版本的不同,决定因素也不一样,掌握这些因素对了解hadoop分片的划分有很大帮助, 并且对优化hadoop性能也很有大的益处. 旧API中getSpli ...
- NET npoi 合并单元值处理
获取sheet中存在合并单元格总数,循环绑定值 // 得到一个sheet中有多少个合并单元格 int sheetMergeCount = sheet.NumMergedRegions; ; i < ...
- 开源应用框架BitAdminCore:更新日志20180817
索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...
- C博客第01次作业---顺序,分支结构
1.本章学习总结 1.1 思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 经过了这一周的学习,从一开始对C语言一无所知,到现在能够写出基本的代码,感到非常开心. 学习C语言也并非想象 ...
- Ceph 的基础数据结构 [Pool, Image, Snapshot, Clone]
原文链接:http://www.cnblogs.com/sammyliu/p/4843812.html?utm_source=tuicool&utm_medium=referral 1 Poo ...
- LOJ#6045. 「雅礼集训 2017 Day8」价(最小割)
题面 传送门 题解 首先先把所有权值取个相反数来求最大收益,因为最小收益很奇怪 然后建图如下:\(S\to\)药,容量\(\inf+p_i\),药\(\to\)药材,容量\(\inf\),药材\(\t ...
- Codeforces Global Round 2部分题解
传送门 好难受啊掉\(rating\)了-- \(A\ Ilya\ and\ a\ Colorful\ Walk\) 找到最后一个与第一个颜色不同的,比一下距离,然后再找到最左边和最右边与第一个颜色不 ...
- flask 安装
flask官网 : http://docs.jinkan.org/docs/flask/installation.html (基本上就是按照官网思路一点一点来的) 1,安装easy_install: ...