Spring使用笔记(四) 面向切面的Spring
面向切面的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的更多相关文章
- Spring学习(四)--面向切面的Spring
一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...
- Spring系列(四) 面向切面的Spring
除了IOC外, AOP是Spring的另一个核心. Spring利用AOP解决应用横切关注点(cross-cutting concern)与业务逻辑的分离, 目的是解耦合. 横切关注点是指散布于代码多 ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...
- 第04章-面向切面的Spring
1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...
- 五、面向切面的spring(1)
spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...
- 面向切面的Spring
在软件开发中,发布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(但往往直接嵌入到应用的业务逻辑之中).将横切关注点与业务逻辑相分离是AOP所要解决的. 一 ...
- Spring AOP 面向切面的Spring
定义AOP术语 描述切面的常用术语有: 通知 (advice) 切点 (pointcut) 连接点 (joinpoint) 下图展示了这些概念是如何关联的 Spring 对AOP的支持 Spring提 ...
- Spring学习笔记(三):面向切面的Spring
Spring之面向切面编程 一.理解何为面向切面编程 对于这个的理解,我觉得Spring实战中的例子讲得很明白: 假设我现在是一个小区用户,每个月小区都要收电费,这时候就会来人查看电表,算出来这个月电 ...
- 2015年12月13日 spring初级知识讲解(四)面向切面的Spring
2015年12月13日 具体内容待补充...
随机推荐
- Python变量的作用域
局部变量 局部变量是指在函数内部定义并使用的变量,他只在函数内部有效.即函数内部的名字只在函数运行时才会创建,在函数运行之前或者运行完毕之后,所有的名字就都不存在了.所以,如果在函数外部使用函数内部定 ...
- .tar.xz文件的解压方法
废话不多说: 直接看 方法一: tar -xvJf ***.tar.gz 方法二: 先减压成 .tar 格式的文件, 再解压 .tar #xz是一个工具, 系统中没有安装,需要下载 xz -d *** ...
- Eclipse编写ExtJS5卡死问题
本篇以eclipse为例,导入后在编译时很容易出现eclipse的卡死现象,这主要是js文件的校验引起的. 我们可通过如下方法进行配置: 打开该项目的.project文件,删除如下配置即可: < ...
- 调试WebApi的一些方法
1.Get方法时,直接用浏览器访问 2.Postman 3.用HttpClient调用 privatevoid GetData() { using (HttpClient client = new H ...
- MySQL 复制表到另一个表
1.复制表结构及数据到新表 create table 新表 select * from 旧表 2.只复制表结构到新表 方法1:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已 ...
- [转] React Hot Loader 3 beta 升级指南
前言 在用 react-hot-loader v1.3 的时候有些深层组件不会很完美的热更新(可能是我使用有问题).然后在 react-hot-loader 首页中看到 React Hot Loade ...
- noi.ac 第五场第六场
t1应该比较水所以我都没看 感觉从思路上来说都不难(比牛客网这可简单多了吧) 第五场 t2: 比较套路的dp f[i]表示考虑前i个数,第i个满足f[i]=i的最大个数 i能从j转移需要满足 j< ...
- nethogs命令执行报异常的解决方法
- kudu的写数据流程
写入操作是指需进行插入.更新或删除操作的一组行.需要注意的事项是Kudu强制执行主关键字的唯一性,主关键字是可以更改行的唯一标识符.为了强制执行此约束条件,Kudu必须以不同的方式处理插入和更新操作, ...
- ELK使用3-Logstash
一.命令行输入输出操作 1.命令行输出: /application/elk/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} ...