AOP:面向切面编程,相当于OOP面向对象编程。

Spring的AOP的存在目的是为了解耦,AOP可以让一组类共享相同的行为。

Spring支持AspectJ的注解切面编程:

(1)使用@Aspect声明是一个切面

(2)使用@Afte、@Before、@Around定义通知/建言,可以直接将拦截规则(切点)作为参数。

(3)为了使切点服用,可以使用@PointCut专门定义拦截规则,然后在@After、@Before、   @Advance的参数中调用。

(4)其中符合条件的每一个被拦截处为 连接点(JoinPoint)

eg:

 package com.wisely.heighlight_spring4.ch1.aop;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
*
* Makedate:2018年3月21日 下午1:01:48
* @author dong
* @version %I%, %G%
* @since 1.0
* 注解类(jdk1.5之后没设定特殊关键字而已)
* 功能:拦截规则的注解
* retention:保留 policy:政策 方针
*
*
**/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.stereotype.Service;

 @Service
public class DemoAnnotationService {
@Action(name="注解式拦截的add操作")
public String add() {
return "haha1";
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.stereotype.Service;

 /**
*
* 编写使用方法规则被拦截类
*
* Makedate:2018年3月21日 下午3:08:39
* @author dong
* @version %I%, %G%
* @since 1.0
*
*
*/
@Service
public class DemomethodService {
public String add() {
return "hehe2";
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import java.lang.reflect.Method;

 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; @Aspect //注解声明是一个切面 切面=通知+切点
@Component //让这个切面成为spring容器管理的Bean
public class LogAspect {
@Pointcut("@annotation(com.wisely.heighlight_spring4.ch1.aop.Action)") //注解声明切点
public void annotationPointCut() {}; @After("annotationPointCut()") //注解声明通知 并使用@PointCut定义的切点
public void after(JoinPoint joinpoint) {
MethodSignature signature = (MethodSignature)joinpoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("注解式拦截:"+action.name()); //通过反射可以获得注解上的属性,做日志记录相关操作
} @Before("execution(* com.wisely.heighlight_spring4.ch1.aop.DemomethodService.*(..))")
//@Before声明一个通知 这个通知直接使用拦截规则作为参数
public void before(JoinPoint joinpoint) {
MethodSignature signature = (MethodSignature)joinpoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截,"+method.getName());
}
}
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration
@ComponentScan("com.wisely.heighlight_spring4.ch1.aop")
@EnableAspectJAutoProxy //开启spring对aspecJ的支持
public class AopConfig { }
 package com.wisely.heighlight_spring4.ch1.aop;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;

 public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemomethodService demomethodService = context.getBean(DemomethodService.class);
String add = demoAnnotationService.add();
System.out.println("add---"+add); String add2 = demomethodService.add();
System.out.println("add2---"+add2);
context.close();
}
}

SpringBoot之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. python值json与pickle模块

    #json 是用来序列化对象的 # 只有2个方法,序列化与反序列化 # 但是不能序列化类 与 函数 import json dict={"key1":[1,2,3,4,5]} f ...

  2. 【论文速读】Shangbang Long_ECCV2018_TextSnake_A Flexible Representation for Detecting Text of Arbitrary Shapes

    Shangbang Long_ECCV2018_TextSnake_A Flexible Representation for Detecting Text of Arbitrary Shapes 作 ...

  3. CAN双机通讯调试小结(SJA1000与MCP2515通讯)

    2011-12-07 21:36:02. 效果图: 1,51的SJA1000自收自发测试完成,见上一篇小结. 2,SJA1000自测完成后,再自测MCP2515就非常容易.主要是设置工作模式为回环模式 ...

  4. [flutter+dart] windows7下开发环境的安装与配置

    前言 博主是做嵌入式的,参加工作时间也不久,而且是非科班出身,之前从未接触过移动开发.最近了解到了flutter框架和dart语言,想作为第二语言学习一下,因此会从最基础的环节开始,以此博客作为记录, ...

  5. JZ2440学习笔记之内存设备

    通过OM[1:0]选择启动的设备: OM[1:0]=00,地址0对应的是Internal 4K RAM,且Nand的前4K会被复制到这里,得到执行: OM[1:0]=01,地址0对应的是Nor Fla ...

  6. 自动弹出pickerview

    UIPickerView是开发中常用的控件,日期选择.年龄选择.城市的多级联动等等都会使用,它一般是在点击某个按钮后出现,展现方式和UITextView一样,从页面底部弹出,选中后或者点击控件以外区域 ...

  7. kali linux安装教程及VMware Tool工具的安装

    一.Kali Linux在VMware下的安装 kali系统的简介 1.Kali Kali Linux是基于 Debian 的 Linux发行版,设计用于数字取证和渗透测试的操作系统.由Offensi ...

  8. JsTree 最详细教程及完整实例

    JsTree是一个jquery的插件,它提交一个非常友好并且强大的交互性的树,并且是完全免费或开源的(MIT 许可).Jstree技持Html 或 json格式的的数据, 或者是ajax方式的动态请求 ...

  9. JS(JavaScript)的进一步了解2(更新中···)

    js数据类型 基本数据类型:string   undefined   null  boolean  number 引用数据类型  Object  array  function 二者的区别 基本数据类 ...

  10. Java利用TCP编程实现简单聊天室

    前言: 本文是我在学习尚学堂JAVA300集第二季网络编程部分仿照视频内容实现而成 具体可以去尚学堂官网观看视频学习 一.实现思路 实现聊天室的最核心部分就是JAVA的TCP网络编程. TCP 传输控 ...