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. [转]RTH试用手记之“额外功能”

    年初,罗德与施瓦茨公司(Rohde & Schwarz)推出了第一款的手持示波器,从指标上看,该示波器打破了传统手持器功能简单.指标水平低.结构粗糙的印象,取而代之达到了主流台式数字示波器的性 ...

  2. String调用Array相关方法——有点古怪

    这个系列的前面几篇文章中有谈到在一个Object上使用apply.call等方法操作另一个Object的方法,今天我们来学习怎么样在String上调用Array相关方法. 在许多方面,字符串表现的好像 ...

  3. Easy前端正确删除datagrid的方式(避免直接删除索引没更新问题)

    在删除传参时,不要传索引来删除行 columns: [[ { title: '代码', field: 'Code', width: 100 }, { title: '名称', field: 'Name ...

  4. struts1.x和struts2.x之间的一些区别

    转载自http://blog.csdn.net/john2522/article/details/7436307/ struts2不是struts1的升级,而是继承的webwork的血统,它吸收了st ...

  5. JS控制输入框,输入正确的价格

    在HTML中,验证输入内容的正确性是提高用户体验的一方面,同时也是初步保证了数据的来源的正确性. 下面是一个常用的控制输入正确的价格的JS代码 function clearNoNum(obj) { o ...

  6. Android应用安全解决方案

    Apk安全解决方案 背景 公司为政府做的App开发完了,需要上一些手段保证安全.这样客户才放心嘛. 防止第三方反编译篡改应用,防止数据隐私泄露,防止二次打包欺骗用户. 目录 Apk安全解决方案 背景 ...

  7. C#中==操作符存在的缺陷

    ==操作符因为语法简洁而备受欢迎,但它本身也存在着局限性,比如继承或泛型问题.下面让我们依次来看看吧. 1.==和继承性问题 关于==运算符在继承时存在的问题,我们以String类型为例进行说明. s ...

  8. C# 时间戳与DateTime间的互相转换

    //DateTime转换为时间戳public long GetTimeSpan(DateTime time) { DateTime startTime = TimeZone.CurrentTimeZo ...

  9. MvvmLight框架使用入门(二)

    上一篇我们简单对MvvmLight做了介绍.罗列了三个DLL中,各个命名空间下主要类的定义及大致作用.因为只是范范的概论,对于从未接触过MvvmLight的萌新来说,根本就是在晃点他们.不过万事开头难 ...

  10. Android 中 LayoutParams 的用法

    一个控件应当使用它的父控件的 LayoutParams 类型.因此,一个 TableVow 应该使用 TableLayout.Params . 所以,以一个 TableRow 为例: TableRow ...