面向切面的Spring

一、面向切面的概念

在软件开发中,散布于应用多处的功能被称为横切关注点(cross-cutting concern)。

通常来讲这些横切关注带点从概念上来讲是与应用逻辑相分离的(但是往往会直接嵌入到应用的业务逻辑中)。

把这些横切关注点与业务逻辑相互分离正是面向切面编程(AOP)所要解决的问题。

切面所适用的场景:日志、声明式事务、安全和缓存。

横切关注点可以被模块化特殊的类,这些类被称为切面。

1.相关术语

1)通知(advice):描述了切面的工作及如何工作。通知的类型:

前置通知(Before)

后置通知(After)

返回通知(After-returning)

异常通知(After-throwing)

环绕通知(方法调用前后都执行)

2)连接点(join-point):应用执行时可以插入的一个点,如方法调用时,异常抛出时。

3)切点(cut-point):切点的定义会匹配通知所要植入的一个或者多个链接点。

4)切面(Aspect):切面是通知和切点的结合,通知和切点共同定义切面的全部内容----

它是什么及何时何地完成其内容。

5)引入(Introduction):

6)织入(Weaving):把切面应用到目标对象,并创建新的代理的过程。

2.Spring对AOP的支持

Spring提供了4种类型的AOP支持

1.基于代理的经典SpingAOP

2.纯POJO切面

3.@AspectJ注解驱动的切面

4.注入式AspectJ切面

前三个都是Spring AOP实现的变体,Spring AOP构建在动态代理的基础上,

因此,Spring对AOP的支持仅仅限于方法层面。

1.示例使用JavaConfig: 

public interface Performance {
void performance();
}
public class Actor implements Performance{
public void performance() {
System.out.println("An actor is on the stage!!!~!!");
}
}
@Aspect
public class Audience {
//定义命名的切点
@Pointcut("execution(* entity.Actor.performance(..))")
public void performance() {} @Before("performance()")
public void silence() {
System.out.println("Silence please!");
} @Before("performance()")
public void takeSeats() {
System.out.println("Take seats please!");
} @AfterReturning("performance()")
public void performanceOver() {
System.out.println("Performance Over!");
} @AfterThrowing("performance()")
public void wrong() {
System.out.println("Something wrong with the show!");
} /*
* 在一个通知方法中同时编写前置和后置通知
* */
@Around("performance()")
public void around(ProceedingJoinPoint pjp) {
System.out.println("Around!! before");
try {
pjp.proceed(); //必须调用该方法,如果不调用会阻塞除了
// AfterReturning以外的其他方法
System.out.println("Around!! after");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Aconfig.class)
public class TestAll {
@Autowired
private Performance actor;
@Test
public void ts() {
actor.performance();
/*Around!! before
Silence please!
Take seats please!
An actor is on the stage!!!~!!
Around!! after
Performance Over!*/
}
}
@Configuration
@EnableAspectJAutoProxy //启用自动代理功能
public class Aconfig {
@Bean
public Audience audience() {
return new Audience();
} @Bean
public Performance actor() {
return new Actor();
}
}

2.示例通过XML:

@Aspect
public class Audience {
public void silence() {
System.out.println("Silence please!");
}
public void takeSeats() {
System.out.println("Take seats please!");
}
public void performanceOver() {
System.out.println("Performance Over!");
}
public void wrong() {
System.out.println("Something wrong with the show!");
}
/*
* 在一个通知方法中同时编写前置和后置通知
* */
public void around(ProceedingJoinPoint pjp) {
System.out.println("Around!! before");
try {
pjp.proceed(); //必须调用该方法,如果不调用会阻塞除了
// AfterReturning以外的其他方法
System.out.println("Around!! after");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标对象-->
<bean id="actor" class="entity.Actor" />
<!--切面对象-->
<bean id="audience" class="entity.Audience"/>
<!--切面-->
<aop:config>
<!--定义命名的切点-->
<aop:pointcut expression="execution(* entity.Actor.performance(..))" id="cutRef"/>
<aop:aspect ref="audience">
<aop:before method="silence" pointcut-ref="cutRef"/>
<aop:before method="takeSeats" pointcut-ref="cutRef"/>
<aop:after-returning method="performanceOver" pointcut-ref="cutRef"/>
<aop:after-throwing method="wrong" pointcut-ref="cutRef"/>
<aop:around method="around" pointcut-ref="cutRef"/>
</aop:aspect>
</aop:config>
<aop:aspectj-autoproxy/><!--启用自动代理-->
</beans>
@Autowired
private Performance actor;
@Test
public void ts() {
actor.performance();
/*Silence please!
Take seats please!
Around!! before
An actor is on the stage!!!~!!
Around!! after
Performance Over!*/
}

3.处理通知中的参数 

Spring使用笔记(四) 面向切面的Spring的更多相关文章

  1. Spring学习(四)--面向切面的Spring

    一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...

  2. Spring系列(四) 面向切面的Spring

    除了IOC外, AOP是Spring的另一个核心. Spring利用AOP解决应用横切关注点(cross-cutting concern)与业务逻辑的分离, 目的是解耦合. 横切关注点是指散布于代码多 ...

  3. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  4. 第04章-面向切面的Spring

    1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...

  5. 五、面向切面的spring(1)

    spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...

  6. 面向切面的Spring

    在软件开发中,发布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(但往往直接嵌入到应用的业务逻辑之中).将横切关注点与业务逻辑相分离是AOP所要解决的. 一 ...

  7. Spring AOP 面向切面的Spring

    定义AOP术语 描述切面的常用术语有: 通知 (advice) 切点 (pointcut) 连接点 (joinpoint) 下图展示了这些概念是如何关联的 Spring 对AOP的支持 Spring提 ...

  8. Spring学习笔记(三):面向切面的Spring

    Spring之面向切面编程 一.理解何为面向切面编程 对于这个的理解,我觉得Spring实战中的例子讲得很明白: 假设我现在是一个小区用户,每个月小区都要收电费,这时候就会来人查看电表,算出来这个月电 ...

  9. 2015年12月13日 spring初级知识讲解(四)面向切面的Spring

    2015年12月13日 具体内容待补充...

随机推荐

  1. You are my brother

    问题 : You are my brother 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Little A gets to know a new friend, Little B, ...

  2. JS去除空格和换行的正则表达式(推荐)

    //去除空格  String.prototype.Trim = function() {  return this.replace(/\s+/g, "");  }    //去除换 ...

  3. 用HBuilderX 打包 vue 项目 为 App 的步骤

    首先打包你的 vue 项目 生成 dist 文件夹,教程请移步  https://www.cnblogs.com/taohuaya/p/10256670.html 看完上面的教程,请确保 你是 将: ...

  4. CHENGDU1-Python编程语言和PEP8规范

    CHENGDU1-Python编程语言和PEP8规范 PEP8规范6条? 答:PEP8规范说白了就是一种规范,可以遵守,也可以不遵守,遵守PEP8可以让代码的可读性更高. 代码编排:---缩进,4个空 ...

  5. CTPN项目部分代码学习

    上次拜读了CTPN论文,趁热打铁,今天就从网上找到CTPN 的tensorflow代码实现一下,这里放出大佬的github项目地址:https://github.com/eragonruan/text ...

  6. 支持向量机-SMO算法简化版

    SMO:序列最小优化 SMO算法:将大优化问题分解为多个小优化问题来求解 SMO算法的目标是求出一系列的alpha和b,一旦求出这些alpha,就很容易计算出权重向量w,并得到分隔超平面 工作原理:每 ...

  7. STL容器之优先队列

    STL容器之优先队列 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关键字是priority_queue(太伤了).在一些定义了权重的地方这个数据结构是很有用的. 先回顾队列的定义:队列 ...

  8. Tomcat8 启动慢 Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [53,161] milliseconds

    修改$JAVA_PATH/jre/lib/security/java.security文件 将 securerandom.source=file:/dev/random 修改为 securerando ...

  9. Android 实现连续两次点击或连续多次点击退出应用

    前言:  日常开发过程中,经常会遇到“连续点击两次退出应用”的需求(和“连续点击多次”的需求(如:手机从设置中进入开发者选项). 直接上代码:双击退出: private long exitTime = ...

  10. Docker Client (another java docker client api)

    前一篇提到了docker-java,这里介绍另一个docker client 库,Docker Client 版本兼容 兼容17.03.1~ce - 17.12.1~ce (点 [here][1]查看 ...