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. 使用Docker部署ASP.NET Core应用程序实践

    前言 最近把很火的Docker给看了,于是就磨拳擦掌要去实践一下.于是就拿之前一个aps.net core的项目(已被停止)去练手.该项目之前在ubuntu14.04上确保可以正常运行,所以docke ...

  2. SQL Server查询所有存储过程信息、触发器、索引

    1. [代码]查询所有存储过程      01 select Pr_Name as [存储过程], [参数]=stuff((select ','+[Parameter] 02 from ( 03 se ...

  3. mysql多个TimeStamp设置(转)

    timestamp设置默认值是Default CURRENT_TIMESTAMP timestamp设置随着表变化而自动更新是ON UPDATE CURRENT_TIMESTAMP 但是由于 一个表中 ...

  4. CentOS 7安装OpenVPN

    命令很简单,但是网上似乎没资料,只有RHEL6以下的资料. 直接贴命令: 1 2 3 4 5 cd ~ wget http://swupdate.openvpn.org/as/openvpn-as-2 ...

  5. Hadoop集群 -Eclipse开发环境设置

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  6. Redis配置参数汇总

    ==配置文件全解=== ==基本配置daemonize no 是否以后台进程启动databases 16 创建database的数量(默认选中的是database 0) save 900 1 #刷新快 ...

  7. 浏览器兼容性随手记:Javascript

    1.event IE9以下不支持直接获取event对象,所以需要写兼容: var event = event?event:window.event; IE8以下不支持event.target,但是可以 ...

  8. API Test Postman接口测试之高级篇2

    API Test  Postman接口测试之高级篇2 一.继承父类的设置: 二.导出及导入: 三.分享文档: 四.发布接口文档: 五.常用脚本: 右边框选的是一些常用的脚本,postman提供的,可以 ...

  9. jquery带参数选项卡4

    带参数选项卡: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. JAVA中Date类的使用

    一. Date类 Date类对象的创建: 1.创建一个当前时间的Date对象 //创建一个代表系统当前日期的Date对象 Date d = new Date(); 2.创建一个我们指定的时间的Date ...