学习 Spring (十七) Spring 对 AspectJ 的支持 (完结)
Spring入门篇 学习笔记
@AspectJ 的风格类似纯 java 注解的普通 java 类
Spring 可以使用 AspectJ 来做切入点解析
AOP 的运行时仍旧是纯的 Spring AOP, 对 AspectJ 的编译器或者织入无依赖性
Spring 中配置 @AspectJ
对 @AspectJ 支持可以使用 XML 或 Java 风格的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<context:component-scan base-package="com.karonda.aop.aspectj"/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
确保 AspectJ 的 aspectjweaver.jar (1.6.8或更高版本) 库包含在应用程序的 classpath 中
aspect
@AspectJ 切面使用 @Aspect 注解配置,拥有 @Aspect 的任何 bean 将被 Spring 自动识别并应用
用 @Aspect 注解的类可以有方法和字段,他们也可能包括切入点、通知和引入的声明
@Aspect 注解是不能通过类路径自动检测发现的,所以需要配合使用 @Component 注释或者在 XML 配置 bean
一个类中的 @Aspect 注解标识它为一个切面,并且将自己从自动代理中排除
@Component
@Aspect
public class MoocAspect {
}
pointcut
一个切入点通过一个普通的方法定义来提供,并且切入点表达式使用 @Pointcut 注解,方法返回类型必须为 void
@Component
@Aspect
public class MoocAspect {
@Pointcut("execution(* com.karonda.aop.aspectj.biz.*Biz.*(..))")
public void pointcut() {}
@Pointcut("within(com.karonda.aop.aspectj.biz.*)")
public void bizPointcut() {}
}
组合 pointcut
切入点表达式可以通过 &&, || 和 ! 进行组合,也可以通过名字引用切入点表达式
通过组合,可以建立更加复杂的切入点表达式
定义良好的 pointcut
AspectJ 是编译期的 AOP
检查代码并匹配连接点与切入点的代价是昂贵的
一个好的切入点应该包括以下几点:
- 选择特定类型的连接点,如:execution, get, set, call, handler
- 确定连接点范围,如:within, withincode
- 匹配上下文信息,如:this, target, @annotation
advice
添加类:
@Service
public class MoocBiz {
public String save(String arg) {
System.out.println("MoocBiz save : " + arg);
// throw new RuntimeException(" Save failed!");
return " Save success!";
}
}
添加测试类:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAspectJ extends UnitTestBase {
public TestAspectJ() {
super("classpath:spring-aop-aspectj.xml");
}
@Test
public void test() {
MoocBiz biz = getBean("moocBiz");
biz.save("This is test.");
}
}
Before advice
在 MoocAspect 类添加方法:
// @Before("execution(* com.karonda.aop.aspectj.biz.*Biz.*(..))")
@Before("pointcut()")
public void before() {
System.out.println("Before.");
}
After returning advice
@AfterReturning(pointcut="bizPointcut()", returning="returnValue")
public void afterReturning(Object returnValue) {
System.out.println("AfterReturning : " + returnValue);
}
After throwing advice
@AfterThrowing(pointcut="pointcut()", throwing="e")
public void afterThrowing(RuntimeException e) {
System.out.println("AfterThrowing : " + e.getMessage());
}
After (finally) advice
最终通知必须准备处理正常和异常两种返回情况,它通常用于释放资源
@After("pointcut()")
public void after() {
System.out.println("After.");
}
Around advice
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Around 1.");
Object obj = pjp.proceed();
System.out.println("Around 2.");
System.out.println("Around : " + obj);
return obj;
}
advice 扩展
给 advice 传递参数
args
@Before("pointcut() && args(arg)")
public void beforeWithParam(String arg) {
System.out.println("BeforeWithParam." + arg);
}
annotation
可以用来判断方法上是否加了某注解或者方法上加的注解对应的值
定义一个注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MoocMethod {
String value();
}
添加注解;
@Service
public class MoocBiz {
@MoocMethod("MoocBiz save MoocMethod.")
public String save(String arg) {
System.out.println("MoocBiz save : " + arg);
// throw new RuntimeException(" Save failed!");
return " Save success!";
}
}
添加通知:
@Before("pointcut() && @annotation(moocMethod)")
public void beforeWithAnnotaion(MoocMethod moocMethod) {
System.out.println("BeforeWithAnnotation." + moocMethod.value());
}
advice 的参数及泛型
public interface Sample<T>{
void sampleGenericMethod (T param);
void sampleGenericCollectionMethod (Collection<T> param);
}
@Before("execution(* ..Sample+.sampleGenericMethod(*)) && args(param)")
public void beforeSampleMethod(MyType param){
}
@Before("execution(* ..Sample+.sampleGenericCollectionMethod(*)) && args(param)")
public void beforeSampleMethod(Collection<MyType> param){
}
advice 参数名称
通知和切入点注解有一个额外的 argNames 属性,它可以用来指定所注解的方法的参数名
@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && annotation(auditable)", argNames="bean,auditable")
public void audit(Object bean, Auditable a)
如果第一个参数是 JoinPoint, ProceedingJoinPoint, JoinPoint.StaticPart 那么可以忽略它
@Before(value="com.xyz.lib.Pointcuts.anyPublicMethod() && target(bean) && annotation(auditable)", argNames="bean,auditable")
public void audit(JoinPoint jp, Object bean, Auditable a)
Introduction
允许一个切面声明一个通知对象实现指定接口,并且提供了一个接口实现类来代表这些对象
introduction 使用 @DeclareParents 进行注解,这个注解用来定义匹配的类型拥有一个新的 parent
@Aspect
public class UsageTracking{
@DeclareParents(value="com.xyz.myapp.service.*+", defaultImpl=DefaultUsageTracked.class)
public class UsageTracked mixin;
@Before("com.xyz.myapp.SystemArchitecture.businessService() && this(usageTracked)")
public void recordUsage(UsageTracked usageTracked) {
usageTracked.incrementUseCount();
}
}
切面实例化模型
perthis 切面通过指定 @Aspect 注解 perthis 子句实现
每个独立的 service 对象执行时都会创建一个切面实例
service 对象的每个方法在第一次执行的时候创建切面实例,切面在 service 对象失效的同时失效
@Aspect("perthis(com.xyz.myapp.SystemArchitecture.businessService())")
public class MyAspect{
private int someState;
@Before(com.xyz.myapp.SystemArchitecture.businessService())
public void recordServiceUsage(){
}
}
源码:learning-spring
学习 Spring (十七) Spring 对 AspectJ 的支持 (完结)的更多相关文章
- 学习Spring-Data-Jpa(十七)---对Web模块的支持
Spring-Data还提供了Web模块的支持,这要求Web组件Spring-MVC的jar包位于classpath下.通常通过使用@EnableSpringDataWebSupport注解来启用继承 ...
- (转)实例简述Spring AOP之间对AspectJ语法的支持(转)
Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ.一:使用AspectJ注解:1,启用对AspectJ的支持:通过 ...
- Java缓存学习之五:spring 对缓存的支持
(注意标题,Spring对缓存的支持 这里不单单指Ehcache ) 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache ...
- Spring入门篇——第7章 Spring对AspectJ的支持
第7章 Spring对AspectJ的支持 介绍Spring对AspectJ的支持 7-1 AspectJ介绍及Pointcut注解应用 实例 完成了在xml文件的配置 7-2 Advice定义及实例 ...
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...
- Spring学习(二):Spring支持的5种Bean Scope
序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...
- spring(二) AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- SSM(spring mvc+spring+mybatis)学习路径——1-2、spring事务管理
目录 1-2 Spring事务管理 概念介绍 事务回顾 事务的API介绍 Spring 事务管理 转账案例 编程式事务管理 声明式事务管理 使用XML配置声明式事务 基于tx/aop 使用注解配置声明 ...
- SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇
目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...
随机推荐
- P2678 跳石头题解
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...
- 从MFQ方法到需求分析
前几天看了一篇性能测试相关的文章:性能测试模型初探及应用方法分析,其中提到了MFQ分析方法.专门去查阅了MFQ相关的一些资料,学习了一番. 之后想起了以前看<Google的软件测试之道>这 ...
- Linux环境:持续集成环境部署系列
之前的博客介绍了Linux环境下持续集成环境部署的一些内容,这篇博客将其整理一下,算做一个index,方便自己和大家查阅... 1.linux下安装JDK Jenkins是一个java开源的持续集成工 ...
- Luogu P5168 xtq玩魔塔
这题不错啊,结合了一些不太传统的姿势. 首先看到题目有一问从一个点到另一个点边权最小值.想到了什么? 克鲁斯卡尔生成树+倍增?好吧其实有一个更常用NB的算法叫克鲁斯卡尔重构树 (不会的可以看dalao ...
- 【原创】分布式之redis的三大衍生数据结构
引言 说起redis的数据结构,大家可能对五大基础数据类型比较熟悉:String,Hash,List,Set,Sorted Set.那么除此之外,还有三大衍生数据结构,大家平时是很少接触的,即:bit ...
- ProxySQL+Mysql实现数据库读写分离实战
ProxySQL介绍 ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎.具有以下特性:http://www.proxysql.com/ 1.连接池,而且是multiplexing 2 ...
- MangoDB高级应用
MongoDB高级应用 Author:SimpleWu 聚合 聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). ...
- HBase篇(5)- BloomFilter
[每日五分钟搞定大数据]系列,HBase第五篇.上一篇我们落下了Bloom Filter,这次我们来聊聊这个东西. Bloom Filter 是什么? 先简单的介绍下Bloom Filter(布隆过滤 ...
- win2016 配置IIS 和mysql5.7 迁移数据表的两个小坑
今天配置一整天,就IIS都装了一整天,都是没办法安装.net3.5的问题. 最后解决办法:https://help.aliyun.com/knowledge_detail/38203.html?spm ...
- python爬虫随笔(2)—启动爬虫与xpath
启动爬虫 在上一节中,我们已经创建好了我们的scrapy项目,看着这一大堆文件,想必很多人都会一脸懵逼,我们应该怎么启动这个爬虫呢? 既然我们采用cmd命令创建了scrapy爬虫,那就得有始有终有逼格 ...