Spring框架学习(9)AOP技术理解与使用
内容源自:AOP技术理解与使用
一、什么是AOP?
aop技术是面向切面编程思想,作为OOP(面向对象编程)的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想。
AOP底层也是面向对象;只不过面向的不是普通的Object对象,而是特殊的AOP对象。AOP的关注点是组成系统的非核心通用服务模块(比如登录检查等),相对于普通对象,aop不需要通过继承、方法调用的方式来提供功能,只需要在xml文件中以引用的方式,将非核心服务功能引用给需要改功能的核心业务逻辑对象或方法中。最终实现对象的解耦。spring 中ioc技术实现了核心业务逻辑对象之间的解耦(如LoginAction与DaoImpl), aop技术实现的是核心业务逻辑对象与非核心通用服务之间的解耦(如LoginAction与LoginCheckInterceptor).
二、AOP相对于OOP有什么好处?
OOP的问题不存在于开发阶段,在开发阶段和首次测试阶段中,使用OOP是效率最高也是最简单的一种方式。OOP问题体现在软件开发完毕之后的二次测试阶段,软件修改完毕之后,需要对软件中修改的方法进行重新测试,之后才可以上线运行。这时测试的对象是当前修改的方法 以及 和该方法具有级联/依赖关系的所有的其他方法。这样做显然了延长二次测试周期。 而使用aop在二次测试时,因为他是配置在xml文件中的,所以并不需要测试相关的所有类。
三、spring中如何使用aop?
我们使用一个例子来介绍下如何在spring中使用aop,
这里我们提供一个类StuAction,为这个类中的核心业务逻辑方法(addStu、delStu)添加登录检查的功能。
public class StuAction {
public String addStu(){
System.out.println("处理客户端提交的addStu.action请求");
//int i = 1/0;
return "success";
}
public String delStu(){
System.out.println("处理客户端提交的selStu.action请求");
return "success";
}
}
spring中的AOP技术提供了四种基本类型的通知:
- before通知 ~ 核心方法执行之前的通知 MethodBeforeAdvice
- after通知 ~ 核心方法执行之后的通知 AfterReturningAdvice
- around通知 ~ 核心方法执行时的通知before+after filter、interceptor都是一种around通知 MethodInterceptor
- throws通知 ~ 核心方法执行出现异常之后执行的通知 ThrowsAdvice
这四种通知我们都来测试下:
注意:在测试athrows通知时把StuAction中的int i = 1/0;取消注释,我们创建一个异常来进行测试。
另外,after通知主要用来核心方法调用结束后输出日志,所以这里用到了log4j 。
// 定义一个实现MethodBeforeAdvice的通知类 - before通知
public class LoginCheckAdvice implements MethodBeforeAdvice {
// 该方法会在核心方法执行之前自动执行
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("判断当前是否存在登录用户");
// 根据判断的结果决定是否执行后续的核心方法
}
}
// after通知
public class LoggerAdvice implements AfterReturningAdvice{
private static Logger logger =
Logger.getLogger(LoggerAdvice.class);
// 该方法会在核心方法执行完毕之后自动执行
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("核心方法执行完毕之后进行日志记录");
// 记录一条日志信息
logger.error("这是一条error级别的日志信息");
logger.warn("这是一条warn级别的日志信息");
logger.info("这是一条info级别的日志信息");
logger.debug("这是一条debug级别的日志信息");
}
}
// around通知
public class AroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
//struts2 interceptor ActionInvocation调度者
System.out.println("around通知-核心方法执行之前");
Object result = invocation.proceed();
// 将请求的执行权限转交给核心业务逻辑方法 addStu/selStu
// result - 核心方法的执行结果 addStu()/selStu()
System.out.println("result--"+result); // success
System.out.println("around通知-核心方法执行之后");
return result;
}
}
// throws通知
public class ExceptionAdvice implements ThrowsAdvice {
//该方法会在核心方法执行出现异常之后自动执行
public void afterThrowing(Method method, Object[] args,
Object target, Exception ex){
System.out.println("核心方法执行出现异常了...异常信息"+ex.getMessage());
}
}
log4j.properties
log4j.rootLogger=info,etoak1,etoak2 log4j.appender.etoak1=org.apache.log4j.ConsoleAppender log4j.appender.etoak1.layout=org.apache.log4j.TTCCLayout log4j.appender.etoak2=org.apache.log4j.FileAppender log4j.appender.etoak2.file=C://log4j.html log4j.appender.etoak2.layout=org.apache.log4j.HTMLLayout
好了,准备工作完毕,那我们如何在spring容器中配置aop呢?
重点的来了!
在applicationContext.xml中配置aop:
首先导入spring 中aop下所有包,log4j 包
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--
引入aop命名空间、schame文件
需求 : 使用spring提供的AOP技术为
添加登录检查的辅助功能
1 将登录检查的功能封装成一种spring中的AOP组件
AOP组件[struts2拦截器 filter过滤器 spring通知]
2 将通知类、核心业务逻辑对象[具有依赖关系的两个对象]
配置在ioc容器中
-->
<bean id="action" class="com.etoak.action.StuAction"/>
<bean id="lc" class="com.etoak.util.LoginCheckAdvice"/>
<bean id="logger" class="com.etoak.util.LoggerAdvice"/>
<bean id="around" class="com.etoak.util.AroundAdvice"/>
<bean id="exce" class="com.etoak.util.ExceptionAdvice"></bean>
<!--
3 使用AOP方式将通知类引用给业务逻辑对象
aop:config : 配置一个aop组件[通知类的使用方式]
3.1 描述需要将这个通知类提供的功能引用给谁
aop:pointcut 配置切入点
切入点 : 用于描述通知执行的地点[在哪执行]
地址 : 在哪个/哪些方法周围执行
切入点指向的是需要添加登录检查功能的一组方法
expression属性(表达式):通知执行一个表达式,将表达式的执行结果作为切入点
表达式的执行结果指向的也是一组方法
execution(* com.etoak.action.*.*(..))
execution(1 2) 执行()中表达式的内容
1 用于限定方法的返回值 *
2 用于限定方法的位置、名字
com.etoak.action.*.*(..)
3.2 组装通知类+切入点 形成一个AOP组件[切面]
-->
<aop:config>
<aop:pointcut expression="execution(* com.etoak.action.Stu*.add*(..)) || execution(* com.etoak.action.*.del*(..))" id="pc"/>
<aop:advisor advice-ref="lc" pointcut-ref="pc"/><!-- 将id="lc"这个通知类提供的功能引用给 id="pc"这个切入点指向的那组方法. -->
<aop:advisor advice-ref="logger" pointcut-ref="pc"/>
<aop:advisor advice-ref="around" pointcut-ref="pc"/>
<aop:advisor advice-ref="exce" pointcut-ref="pc"/>
</aop:config>
</beans>
使用test类测试一下:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
StuAction la = (StuAction)ac.getBean("action");
la.addStu();
la.delStu();
}
}
结果如下: 
Spring框架学习(9)AOP技术理解与使用的更多相关文章
- Spring框架中的AOP技术----配置文件方式
1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...
- Spring框架中的AOP技术----注解方式
利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...
- Spring框架学习06——AOP底层实现原理
在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...
- Spring框架学习05——AOP相关术语详解
1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...
- spring框架中的aop技术
1. 什么是AOP, 面向切面编程 AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- spring框架学习(六)AOP
AOP(Aspect-OrientedProgramming)面向方面编程,与OOP完全不同,使用AOP编程系统被分为方面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
随机推荐
- elasticsearch更新license
Elasticsearch更新license: 初次安装Marvel,有30天的使用时间,当到期后,只保存7天的数据,所以需要注册申请一个license: 注册申请地址: https://regist ...
- AngularJS之页面跳转Route
1.除了引用AngularJs.js外,还要引用路由JS, "~/Scripts/angularjs/angular-route.js" 2.通过$routeProvider定义路 ...
- Go语言标准包之json编码
标准的就简单通用. package main import ( "encoding/json" "fmt" "log" ) func mai ...
- CentOS7英文环境下使用中文输入法
一般我们使用英文环境是没有办法使用中文的,这是因为我们没有设置在英文环境下的中文配置,下面我们在英文环境下配置中文输入法ibus使得我们可以输入中文. 首先,安装ibus(centos6以后已经默认安 ...
- OpenStack 计算服务 Nova介绍和控制节点部署 (八)
一)nova在keystone上服务注册 1.1创建nova务实体 [root@controller ~]# source admin-openrc [root@controller ~]# open ...
- RadioGroup多行显示
今天做一个单选框,效果如下: 使用原始的RadioGroup可以实现布局,但因为把6个单选分为两个LinearLayout,导致无法互斥,Google官方的做法是写两个RadioGroup,但是要动态 ...
- 524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- oracle Lower Upper length substr
字符函数: Lower(char):将字符串装换为小写的格式 Upper(char):将字符串装换为大写的格式 length(char):返回字符串的长度 substr(char,m,n):从m开始截 ...
- uva 10910(子集和问题)
Marks Distribution Time limit: 3.000 seconds In an examination one student appeared in N subjects an ...
- HDU 2255 奔小康赚大钱(KM算法)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2255 [题目大意] 求最大匹配 [题解] KM模板 [代码] #include <cstdi ...