跟着刚哥学习Spring框架--AOP(五)
AOP
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。
AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
基于Spring的AOP简单实现
1、通过配置文件实现AOP
首先需要额外上网下载两个jar包:
① aspectjrt.jar
② aspectjweaver.jar
先定义一个接口IHelloWorld:
public interface IHelloWorld {
void printHelloWorld(String name);
}
定义一个实现类HelloWorld
public class HelloWorld implements IHelloWorld {
@Override
public void printHelloWorld(String name) {
System.out.println("Print HelloWorld :" + name);
}
}
实现了两个切面(一个日志切面和一个时间切面)
public class LogAspect {
public void beforeLog(JoinPoint joinPoint){
System.out.println("log before method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
public void afterLog(JoinPoint joinPoint){
System.out.println("log after method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
}
public class TimeAspect {
public void beforeTime(JoinPoint joinPoint){
System.out.println("time before method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
public void afterTime(JoinPoint joinPoint){
System.out.println("time after method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
}
定义一个配置文件
<?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"> <!-- 核心业务Bean -->
<bean id="helloworld" class="com.hzg.HelloWorld"></bean> <!-- 切面Bean(日志切面) -->
<bean id="logAspect" class="com.hzg.LogAspect"></bean> <!-- 切面Bean(时间切面) -->
<bean id="timeAspect" class="com.hzg.TimeAspect"></bean> <!-- AOP配置 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut id="pointcut" expression="execution(* com.hzg.IHelloWorld.*(..))"></aop:pointcut>
<!-- 配置切面(日志切面)切面的优先级需要通过order定义 -->
<aop:aspect id="log" ref="logAspect" order="1">
<!-- 配置通知(前置通知、后置通知、返回通知、异常通知、环绕通知) -->
<aop:before method="beforeLog" pointcut-ref="pointcut"></aop:before>
<aop:after method="afterLog" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
<!-- 配置切面(时间切面)切面的优先级需要通过order定义 -->
<aop:aspect id="time" ref="timeAspect" order="2">
<!-- 配置通知(前置通知、后置通知、后置返回通知、异常通知、环绕通知) -->
<aop:before method="beforeTime" pointcut-ref="pointcut"></aop:before>
<aop:after method="afterTime" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config> </beans>
定义Main主方法
public static void main(String[] args) {
//由于ApplicationContext没有close方法,所以要使用它下面接口ConfigurableApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("configaop.xml");
IHelloWorld helloworld = (IHelloWorld) ctx.getBean("helloworld");
helloworld.printHelloWorld("gangge");
}
输出结果:
log before method printHelloWorld参数 :[gangge]
time before method printHelloWorld参数 :[gangge]
Print HelloWorld :gangge
time after method printHelloWorld参数 :[gangge]
log after method printHelloWorld参数 :[gangge]
2、通过注解实现AOP
实现配置文件方式同样的功能
先定义一个接口文件(没区别)
public interface IHelloWorld {
void printHelloWorld(String name);
}
定义实现类(区别:加了一个注解@Component)
@Component
public class HelloWorld implements IHelloWorld { @Override
public void printHelloWorld(String name) {
System.out.println("Print HelloWorld :" + name);
}
}
定义两个切面(一个日志切面和一个时间切面 区别:增加了@Component、@Aspect和@Order注解)
@Component
@Aspect
@Order(1)
public class LogAspect {
public void beforeLog(JoinPoint joinPoint){
System.out.println("log before method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
public void afterLog(JoinPoint joinPoint){
System.out.println("log after method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
}
@Component
@Aspect
@Order(2)
public class TimeAspect {
@Before("execution(* com.hzg.HelloWorld.*(..))")
public void beforeTime(JoinPoint joinPoint){
System.out.println("time before method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
@After("execution(* com.hzg.HelloWorld.*(..))")
public void afterTime(JoinPoint joinPoint){
System.out.println("time after method " + joinPoint.getSignature().getName() + "参数 :" + Arrays.asList(joinPoint.getArgs()));
}
}
定义配置文件(区别:Spring自动扫描包和aop自动代理)
<?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.hzg"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
定义Main主方法(无区别)
public static void main(String[] args) {
//由于ApplicationContext没有close方法,所以要使用它下面接口ConfigurableApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("configaop.xml");
IHelloWorld helloworld = (IHelloWorld) ctx.getBean("helloworld");
helloworld.printHelloWorld("gangge");
}
输出结果和配置文件一样
五种通知:
1、前置通知
上面已经有了,不再说明
2、后置通知
上面已经有了,不再说明
3、返回通知
/**
* 返回通知: 方法正常执行完后调用,如果有异常,则不调用
* 可以访问到方法的返回值
* @param joinPoint
* @param result 方法的返回值
*/
@AfterReturning(value="execution(* com.hzg.*(..))",returning="result")
public void afterReturning(JoinPoint joinPoint,Object result){
//方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("method "+methodName+" end:"+result);
}
4、异常通知
/**
* 异常通知: 当方法出现异常时
* 可以指定出现哪些异常时才执行: 如何指定?通过入参指定,如:
* 如果入参为NullPointException ex,那么只有在发生空指针异常时才执行
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="execution(* com.hzg.*(..))",throwing="ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex){
//方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("method "+methodName+" occurs:"+ex);
}
5、环绕通知
环绕通知类似于动态代理的全过程,可以使用环绕通知实现前置通知,后置通知,返回通知,异常通知的功能,十分强大,但并不常用
/**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint这个类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值即为目标方法的返回值
* @param pjd
*/
@Around("execution(* com.hzg.*(..))")
public Object around(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName(); try {
//前置通知
System.out.println("method:"+methodName+" begins with "+Arrays.asList(pjd.getArgs()));
//执行目标方法
result = pjd.proceed();
//返回通知
System.out.println("method:"+methodName+" end with "+result);
} catch (Throwable e) {
// 异常通知
System.out.println("method:"+methodName+" occurs exception "+e);
}
//后置通知
System.out.println("method ends");
return result;
}
-------------------------------------------------------------------------------------------------------------------------
跟着刚哥学习Spring框架系列:
跟着刚哥学习Spring框架--创建HelloWorld项目(一)
跟着刚哥学习Spring框架--Spring容器(二)
跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
跟着刚哥学习Spring框架--AOP(五)
跟着刚哥学习Spring框架--JDBC(六)
跟着刚哥学习Spring框架--事务配置(七)
跟着刚哥学习Spring框架--AOP(五)的更多相关文章
- 跟着刚哥学习Spring框架--创建HelloWorld项目(一)
1.Spring框架简介 Spring是一个开源框架,Spring是在2003年兴起的一个轻量级的开源框架,由Rod johnson创建.主要对JavaBean的生命周期进行管理的轻量级框架,Spri ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 跟着刚哥学习Spring框架--Spring容器(二)
Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用. Bean是S ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- 跟着刚哥学习Spring框架--JDBC(六)
Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要提供JDBC模板方式.关系数据库对象化方式.SimpleJdbc方式.事务管理来简 ...
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...
- 23种设计模式之自定义Spring框架(五)
7,自定义Spring框架 7.1 spring使用回顾 自定义spring框架前,先回顾一下spring框架的使用,从而分析spring的核心,并对核心功能进行模拟. 数据访问层.定义UserDao ...
- spring框架 AOP核心详解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
随机推荐
- make/makefile中的加号+,减号-和at号@的含义
http://www.crifan.com/order_make__makefile_in_the_plus__minus_-_and_at_the_meaning_of_numbers/ 在看mak ...
- Java第11章笔记
什么是类,什么是对象 举例说明什么是类,什么是对象? 一句话:万物皆对象 类的概念:类是具有相同属性和服务的一组对象的集合. 1.为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个部分 ...
- Windows事件日志报表 怎样备份数据库?
- swift -inout关键字
一般参数仅仅是在函数内可以改变的,当这个函数执行完后变量就会被销毁,不会有机会改变函数以外的变量,那么我们就会产生一个疑问,我们可不可以通过一个函数改变函数外面变量的值呢?答案是肯定的,这时我们就需要 ...
- pt-query-digest详解慢查询日志
一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...
- nginx反向代理缓存服务器的构建
一:代理服务可简单的分为正向代理和反向代理: 正向代理:用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送给目标Web服务器的HTTP请求先发送 ...
- 2018.11.14 uoj#34. 多项式乘法(fft)
传送门 NOIpNOIpNOIp爆炸不能阻止我搞oioioi的决心 信息技术课进行一点康复训练. fftfftfft板题. 代码: #include<bits/stdc++.h> usin ...
- SQL语句关联查询
一:连接类型: 关联查询:只有存在关联的表才能关联查询,完全独立的表之间无法关联 1.关联的类型:自关联,左关联,右关联,全关联(full join)两张表都是主表 2.关联的表:两张以上,以一张(或 ...
- Lagrange 乘子法求最优解
clc clear syms x y z r1 r2 w f=x^+y^+z^+w^; g1=*x-y+z-w-; g2=x+y-z+w-; h=f-r1*g1 -r2*g2; hx=diff(h,x ...
- GK888CN与Devexpress报表打印标签
安装海鸥驱动,貌似打几张也会报错 使用打印机自带的gk888t驱动,用gk888t(EPL)打带二纬码时会报错 需要选择Togther, xrLable 运行 CanShrink