Spring框架之AOP的基本配置

前言

我们在前面学习了动态代理机制,Spring的AOP正是应用此机制实现的,下面我们来学习如何来配置Spring AOP,实现基本的功能。

什么是AOP

AOP(Aspect Oriented Programming)面向切面编程,是OOP(面向对象编程)的重要补充,面向对象是Java编程的基础,以封装、继承、多态建立了对象间的层次关系,关注的是每个对象的具体实现。面向对象允许我们开发纵向的关系。

AOP关注横向关系,也就是说类和类之间不需要特定的关系,它能够将某些通用的操作(如:日志处理、安全验证、事务处理、异常处理、性能统计等)插入到这些无关的类中,从而可以让每个类只关注自己的核心逻辑,不必处理这些通用的操作,这个过程就是横切,而这些通用的操作就是切面Aspect。

简单来说:AOP能把和类的核心业务无关,多个类又共同需要的逻辑代码封装起来,当对象需要时自动调用,这样就减少了类中的重复代码,降低了类之间的耦合性,提高了程序的维护性。

AOP术语

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

AOP应用场景

1)日志 ,方法执行开始和完成以及出现异常都可以用日志记录

2)缓存 ,第一次调用查询数据库,将查询结果放入内存对象, 第二次调用, 直接从内存对象返回,不需要查询数据库

3)事务 ,调用方法前开启事务, 调用方法后提交关闭事务

等等

AOP的配置

1)导入Maven依赖

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.14.Release</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.8.13</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.13</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

</dependencies>

编写Service接口和实现类,这里只完成模拟操作

public interface AdminService {

void saveAdmin();

void updateAdmin();

void deleteAdmin();

void selectAdmin();

}

public class AdminServiceImpl implements AdminService {

@Override

public void saveAdmin() {

System.out.println("Invoke saveAdmin");

}

@Override

public void updateAdmin() {

System.out.println("Invoke updateAdmin");

}

@Override

public void deleteAdmin() {

System.out.println("Invoke deleteAdmin");

}

@Override

public void selectAdmin() {

System.out.println("Invoke selectAdmin");

}

}

3)编写自定义增强类,该类的方法对应AOP的5种增强通知,分别是:

1)前置通知before :方法调用前执行

2)后置通知after-returning:方法调用后执行,出现异常不执行

3)后置通知after:方法调用后执行,出现异常也执行

4)异常通知after-throwing:出现异常执行

5)环绕通知around:方法调用前后执行

/**

* 自定义增强类

*/

public class MyAdvise {

public void before() throws Throwable {

System.out.println("这是before");

}

public void afterReturning(){

System.out.println("这是afterReturning");

}

public void after(){

System.out.println("这是after");

}

public void afterThrowing() throws Throwable {

System.out.println("这是afterThrowing");

}

public Object around(ProceedingJoinPoint point) throws Throwable {

System.out.println("这是around -- 前置执行");

Object obj = point.proceed();

System.out.println("这是around -- 后置执行");

return obj;

}

}

4) Spring配置文件

<?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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="userService" class="com.qianfeng.springaop.UserServiceImpl"/>

<bean id="adminService" class="com.qianfeng.springaop.AdminServiceImpl"/>

<bean id="myAdvise" class="com.qianfeng.springaop.MyAdvise"/>

<aop:config>

<!--配置切入点

execution中第一个*代表任意返回值类型,后面是包名,第二个*代表类名的开头,第三个*代表任意方法,(..)代表任意方法参数

-->

<aop:pointcut id="c" expression="execution(* com.qianfeng.springaop.*ServiceImpl.*(..))"/>

<!--配置方面-->

<aop:aspect ref="myAdvise">

<aop:before method="before" pointcut-ref="c"/>

<aop:after method="after" pointcut-ref="c"/>

<aop:after-returning method="afterReturning" pointcut-ref="c"/>

<aop:after-throwing method="afterThrowing" pointcut-ref="c"/>

<aop:around method="around" pointcut-ref="c"/>

</aop:aspect>

</aop:config>

</beans>

5)运行测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext-aop.xml")

public class TestAOP {

@Test

public void test(){

ApplicationContext app = new

ClassPathXmlApplicationContext("applicationContext-aop.xml");

AdminService as = (AdminService) app.getBean("adminService");

as.deleteAdmin();

}

}

可以看到执行任何Service类的方法,都会调用MyAdvise中的通知方法

总结

本章我们学习了Spring AOP的配置,首先需要添加通知增强类,可以添加前置、后置或环绕等通知方法来完成某些通用操作,然后再Spring的配置文件中配置切入点,最后是配置切面,将通知类中的方法和切面中的方法进行绑定,最后调用Java对象的方法时,就能看到AOP的效果。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容