1. 走进面前切面编程

编程范式;

面向过程编程,c语言;

面向对象编程;c++,java,c#;

函数式编程;

事件驱动编程;

面向切面编程;

AOP是一种编程范式,不是编程语言;解决特定问题,不能解决所有问题;OOP的补充,不是竞争‘

AOP的初衷:解决代码重复性问题,解决关注点分离;

水平分离;展示层》服务层》持久层;

垂直分离:功能划分 订单库存等;

切面分离:分离功能性需求与非功能性需求;

使用AOP的好处:

集中管理;方便添加删除;增强代码的可读性与可维护性;

AOP的应用场景:

权限控制,缓存控制,事务控制,审计日志,性能监控;分布式追踪;异常处理等;;

支持AOP的编程语言:java,.net,c/c++,ruby python php;

案例:

1.产品管理的服务;

2.产品添加删除的操作只能管理员才能进行;

3.普通实现VSAOP实现;

spring boot 实现;

maven  pom 引入依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
加入product类:
package com.example.aoptest.domain;

public class Product {
private Long id;
private String name; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

  service层代码:

package com.example.aoptest.service;

import com.example.aoptest.domain.Product;
import org.springframework.stereotype.Service; @Service
public class ProductService {
public void insert(Product product){
System.out.println("insert product");
}
public void delete(Long id){
System.out.println("delete product");
}
}

  测试类

package com.example.aoptest.domain;

//模拟用户切换
public class CurrentUserHolder {
private static final ThreadLocal<String> holder = new ThreadLocal<>();
public static String get(){return holder.get() == null?"unknown":holder.get();}
public static void set(String user){holder.set(user);}
}

Service判断

package com.example.aoptest.service;

import com.example.aoptest.domain.CurrentUserHolder;
import org.springframework.stereotype.Component;
@Component
public class AuthService {
public void checkAccess(){
String user = CurrentUserHolder.get();
if(!"admin".equals(user)){
throw new RuntimeException("operation not allow");
}
}
}

  普通传统的校验权限的方式:

进入测试:

执行验证通过;

总结:

传统硬编码的方式的缺点呢:逻辑复杂度较高;

使用AOP

package com.example.aoptest.security;

import com.example.aoptest.service.AuthService;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Aspect
@Component //给spring托管
public class SecurityAspect {
@Autowired
AuthService authService;
//切面,以下表达式有很多种,这里我们用注解的方式
//拦截标注有AdminOnly注解,进行操作;
@Pointcut("@annotation(AdminOnly)")
public void adminOnly(){ } //执行之前插入一段代码。
@Before("adminOnly()")
public void check(){
authService.checkAccess();
}
}

  

package com.example.aoptest.security;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //只标注在方法级别
public @interface AdminOnly {
}

  修改ProductService

//authService.checkAccess();
test测试 正常情况下失败的;因为没有捕获异常
给ProductService加上注解
package com.example.aoptest.service;

import com.example.aoptest.domain.Product;
import com.example.aoptest.security.AdminOnly;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class ProductService {
@Autowired
AuthService authService;
@AdminOnly
public void insert(Product product){
//传统的方式校验
//authService.checkAccess();
System.out.println("insert product");
}
@AdminOnly
public void delete(Long id){
//authService.checkAccess();
System.out.println("delete product");
}
}

  执行测试类,正常通过的。

对比以上两种方式,aop侵入性这种方式更少;

为啥要引入aop的编程范式?
aop的好处及适用场景分别是什么?
aop的两大核心是什么?

												

Spring 学习之AOP的更多相关文章

  1. Spring学习之AOP的实现方式

    Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. spring学习(二) ———— AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  4. Spring学习之AOP

    Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...

  5. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

  6. Spring学习之Aop的基本概念

    转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...

  7. Spring学习之AOP与事务

      一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...

  8. spring学习笔记-AOP

    1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用:   提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...

  9. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

随机推荐

  1. opencv图像读取-imread

    前言 图像的读取和保存一定要注意imread函数的各个参数及其意义,尽量不要使用默认参数,否则就像数据格式出现错误(here)一样,很难查找错误原因的: re: 1.opencv图像的读取与保存; 完

  2. int类型转string类型c++

    前言 使用VS的过程中,经常会用到需要将int类型数据转换为字符串类型,便于显示信息等. 实现方法 c++11标准中的to_string函数,在VS安装文件的include文件中生成的只读文件,使用起 ...

  3. 和菜鸟一起学linux内核源码之基础准备篇

    来源:http://blog.csdn.net/eastmoon502136/article/details/8711104 推荐阅读:linux内核源码最初版linux内核源代码,简单易懂,适合初学 ...

  4. 利用Fierce2查询子域名

    http://pnig0s1992.blog.51cto.com/393390/368428 安装方法引用Mickey的: 1.Mickey@pentestbox:/pentest/enumerati ...

  5. python去掉字符串'\xa0'

    AssertionError: '5\xa0e\xa0*\xa0*\xa0*\xa05' != '5e***5'mystr = '5\xa0e\xa0*\xa0*\xa0*\xa05'mystr = ...

  6. HDU 1251:统计难题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意不难理解,就是先输入不知道多少个字符串,然后用一个空行结束这个输入,然后接下来不知道多少行再 ...

  7. SUST OJ 1642: 绝地求生—死亡顺序

    1642: 绝地求生-死亡顺序 时间限制: 1 Sec  内存限制: 128 MB提交: 81  解决: 53[提交][状态][讨论版] 题目描述 最近陕西科技大学六公寓的小东同学深深的入迷了一款游戏 ...

  8. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  9. 字符串匹配--(K)MP模板

    大白书型模板,并没有写成函数形式: 这个是下标从0开始的: 此外,对于一个长度为 m 的串自匹配,在此模板中 m-p[m] 就是这个串的最小循环节长度(最后一个循环可以不完整) 另一个关于自匹配后的失 ...

  10. m mm等和envsetup.sh

    envsetup.sh简介: Android 完成编译的时候先执行 source build/envsetup.sh.在这个shell 脚本中定义了 help, croot, m, mm, mmm 等 ...