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. Android-ImageUtils工具类

    图片相关的工具类 public class ImageUtils { public static boolean saveImage(Bitmap photo, String spath) { try ...

  2. PipelineDB On Kafka

    PipelineDB 安装yum install https://s3-us-west-2.amazonaws.com/download.pipelinedb.com/pipelinedb-0.9.8 ...

  3. XML--将XML中数据提取出转换成表

    DECLARE @xml XMLSET @xml='<Students>    <Student  id="1001">        <name&g ...

  4. WinRT支持GB2312

    在SL年代,有第三方类库支持 http://encoding4silverlight.codeplex.com 在RT版本有点不兼容,一直没时间看.今天闲的卵疼,就来一段代码兼容一下RT版本,迟点整合 ...

  5. WP8启动您已发布的应用

    您可以使用来自 Windows.Phone.Management.Deployment 命名空间的 API 来确定,来自您的发布者 ID 的其他应用是否安装在手机上.如果已经安装,您也可以使用该 AP ...

  6. 如何将JPG格式的图片转化为带地理坐标的TIFF格式

    最近有个项目需要用到开源软件GeoServer,数据源是一张高分辨率的2.5维图片,格式是jpg的,由于GeoServer不支持jpg格式的发布,因此考虑到要进行格式转换,将其转换成tiff格式. 1 ...

  7. 基于SSH的客户关系管理系统CRM-JavaWeb项目-有源码

    开发工具:Myeclipse/Eclipse + MySQL + Tomcat 项目简介: 项目的编译和运行:1 将数据库导入MysSql里 :打开HeidiSql这个图形化工具,新建一个数据库, 可 ...

  8. django系列3.4-- request对象和response对象(未完待续)

    一.request对象 详细信息可以查阅django官方文档 共有五种请求相关的常用值 request.path_info 返回用户访问的url不包括域名 request.method 请求中使用的H ...

  9. python网络编程--socket,网络协议,TCP

    一. 客户端/服务端架构(用到网络通信的地方) 我们使用qq.微信和别人聊天,通过浏览器来浏览页面.看京东的网站,通过优酷.快播(此处只是怀念一下)看片片啥的等等,通过无线打印机来打印一个word文档 ...

  10. mysql 行转列 (结果集以坐标显示)

    create table capacity( type int , numbers int , monthst INT ); select type, sum(case monthst when 1 ...