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的更多相关文章

  1. Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用

    ==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...

  2. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  3. SpringBoot切面Aop的demo简单讲解

    前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解. SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 切面(Aop) 一.概 ...

  4. Spring全家桶——SpringBoot之AOP详解

    Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...

  5. SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.

    对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...

  6. (办公)springboot配置aop处理请求.

    最近项目用到springboot,就是需要配置一些东西.比如用aop处理请求.方法前通知获取url,method,ip,类方法,参数,方法后通知,返回参数,而且还可以记录一下日志.下面是操作的代码. ...

  7. SpringBoot系列——aop 面向切面

    前言 项目中我们经常会用到aop切面,比如日志记录:这里简单记录一下springboot是如何使用aop spring对aop的配置,来自springboot参考手册,Common applicati ...

  8. SpringBoot使用AOP

    本文介绍SpringBoot中使用Spring AOP. 简介 AOP简介 AOP可能对于广大开发者耳熟能详,它是Aspect Oriented Programming的缩写,翻译成中文就是:面向切面 ...

  9. Spring全家桶系列–SpringBoot之AOP详解

    //本文作者:cuifuan //本文将收录到菜单栏:<Spring全家桶>专栏中 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关 ...

  10. SpringBoot配置Aop笔记【例子】

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

随机推荐

  1. Postgres 主从配置(五)

    PostgreSQL 9.4 新增的一个特性, replication slot,  1. 可以被流复制的sender节点用于自动识别它xlog中的数据在下面的standby中是否还需要(例如, st ...

  2. Buffer Pool--内存总结1

    物理地址空间是处理器用来访问位于总线上的所有部件的集合.在32位处理器上,地址总线为32位,寻址空间为4GB.在使用PAE的32位服务器上,地址总线为36位,寻址空间为64GB.在64位的处理器上生产 ...

  3. .net 开发者尝试Apache Spark™

    本文编译自一篇msdn magazine的文章,原文标题和链接为: Test Run - Introduction to Spark for .NET Developers https://msdn. ...

  4. SQL多行字符串按条件合并

    USE [ARTEA.MES]GO /****** Object: UserDefinedFunction [dbo].[UnionPart] Script Date: 11/18/2015 15:3 ...

  5. C# SQLite 数据库

    数据库 Oracle.Oracle的应用,主要在传统行业的数据化业务中,比如:银行.金融这样的对可用性.健壮性.安全性.实时性要求极高的业务 MS SQL Server.windows生态系统的产品, ...

  6. WM_COPYDATA+BHO+Qt实现进程间通信

    最近项目有一个需求:点击网页上某个按钮,通知Qt客户端.网页相关操作使用了BHO,BHO与Qt通信通过WB_COPYDATA,为什么这么麻烦呢,因为项目正好用到了BHO,可能还有其他方式,能直接通过网 ...

  7. 跨域处理之Jsonp

    一.认识Jsonp JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式 ...

  8. Python 错误和异常小结

    1.Python异常类 Python是面向对象语言,所以程序抛出的异常也是类.常见的Python异常有以下几个,大家只要大致扫一眼,有个映像,等到编程的时候,相信大家肯定会不只一次跟他们照面(除非你不 ...

  9. GO学习笔记 - map

    map是GO语言中的一种高级数据类型,特点是key和value对应,这和Delphi中的Dictionary一样!map的声明格式:map[key数据类型]value数据类型.使用map前,必须用ma ...

  10. 一次mysql调优过程

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 前几天进行了一个数据库查询,比较缓慢,便查询了一下,在这里记录一下,方便 ...