Spring4面向切面AOP
AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术。AOP是OOP的补充,是spring框架中的一个重要内容。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。AOP可以分为静态织入与动态织入,静态织入即在编译前将需织入内容写入目标模块中,这样成本非常高。动态织入则不需要改变目标模块。Spring框架实现了AOP,使用注解配置完成AOP比使用XML配置要更加方便与直观。
AOP基本概念:
切面(aspect):横切关注点被模块化的特殊对象。
通知(advice):切面必须要完成的工作。切面中的每个方向称之为通知。通知是在切面对象中的。 (前置通知、后置通知、返回通知、异常通知、环绕通知)
目标(target):被通知的对象。
代理(proxy):向目标对象应用通知后创建的对象。
连接点(joinpoint):目标对象的程序执行的某个特定位置。如某个方向调用前,调用后的位置。包括两个信息:1.目标程序的哪个方法?2.方法执行前还是执行后?
切点(pointcut):每个类会有多个连接点,AOP通过切点定位到特定的边接点。
需要用到的包:spring的包 和一个aspectjweaver.jar
:
在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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 自动扫描-->
<context:component-scan base-package="com.itnba.entities,com.itnba.aop" ></context:component-scan> <!-- 启动第三方的aspectjweaver.jar中的功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
一种是通过注解的方式,在代码中加入注解,一种是基于xml配置的方式
1、使用注解的方式:
(1)做一个类,做切面类和切面方法。把这个类放在IOC容器中(自动扫描) 为这个类加上@Compoent注解
package com.itnba.entities; import org.springframework.stereotype.Component; @Component
public class Chinese implements IHuman { @Override
public void eat() {
System.out.println("中国人正在吃饭");
} @Override
public void sleep() { System.out.println("中国人正在睡觉");
} }
2、声明某个类为切面,为该类加上@Aspect注解,
在这个类中的方法上,声明方法的前置、后置通知、返回通知、异常通知、环绕通知
package com.itnba.aop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect
@Component
public class HumanAOP { @Before("execution(* com.itnba.entities..*.eat(..))")
public void beforeEat(){ System.out.println("吃饭前洗手吧"); } @AfterReturning("execution(* com.itnba.entities..*.eat(..))")
public void afterEat(){
System.out.println("吃完饭漱口");
}
@AfterThrowing(value="execution(* com.itnba.entities..*.eat(..))",throwing="ex")
public void except( JoinPoint point, Exception ex ){
System.out.println(point.getSignature().getName());
System.out.println(ex.getMessage());
System.out.println("吃饭噎死了,快去抢救");
} @Before("execution(* com.itnba.entities..*.sleep(..))")
public void beforeSleep(){ System.out.println("洗澡后再睡觉"); }
}
通知分类:
通知分类:
前置通知(@Before) -- 在目标方法执行前执行的通知。
后置通知(@After) -- 在目标方法执行后执行的通知。无论是否发生异常。后置通知中,无法读取目标方法返回的结果。
返回通知(@AfterReturnning) --在目标方法执行成功后执行的通知。在返回通知中可以访问目标返回结果的。
@AfterReturnning(value="execution(* com.itnba..*(..))",returning="result")
public void afterRun(Object result){
System.out.println(result);
} 异常通知(@AfterThrowing) -- 在目标方法执行出错后执行的通知。
@AfterThrowing(value="execution(* com.itnba..*(..))",throwing="ex")
public void afterThrowing(Exception ex){
System.out.println(ex.getMessage());
} 环绕通知(@Around) -- 需要切面方法携带ProceedingJoinPoion参数,类似于一个完整的动态代理,环绕通知必须要有一个返回值,是目标方法的返回值。 @Around("execution(* com.itnba..*(..))")
public object aroundAdvice( ProceedingJoinPoint pjp){ object obj = null;
try{
//做前置通知
obj = pjp.proceed();
//做返回通知
}
catch(Exception ex){
//做异常通知
}
//做后置通知
return obj;
}
2、通过xml配置的方式
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-autowire="byName"
> <bean id="chinese" class="com.itnba.entities.Chinese"></bean>
<bean id="american" class="com.itnba.entities.American"></bean>
<bean id="humanAOP" class="com.itnba.aop.HumanAOP"></bean> <aop:config>
<aop:pointcut expression="execution(* com.itnba.entities.*.eat(..))" id="beforeEat"/>
<aop:pointcut expression="execution(* com.itnba.entities.*.eat(..))" id="afterEat"/> <aop:aspect id="ha" ref="humanAOP">
<aop:before method="beforeEat()" pointcut-ref="beforeEat"/>
<aop:after method="afterEat()" pointcut-ref="afterEat"/>
</aop:aspect> </aop:config> </beans>
Spring4面向切面AOP的更多相关文章
- Java 面向切面 AOP
参考: :http://www.blogjava.net/supercrsky/articles/174368.html AOP: Aspect Oriented Programming 即面向切面编 ...
- Spring框架使用(控制反转,依赖注入,面向切面AOP)
参见:http://blog.csdn.net/fei641327936/article/details/52015121 Mybatis: 实现IOC的轻量级的一个Bean的容器 Inversion ...
- Spring框架系列(五)--面向切面AOP
背景: 当需要为多个不具有继承关系的对象引入一个公共行为,例如日志.权限验证.事务等功能时,如果使用OOP,需要为每个对象引入这些公共 行为.会产生大量重复代码,并且不利用维护.AOP就是为了解决这个 ...
- 解析Spring第三天(面向切面AOP)
面向切面:AOP 在不修改源代码的基础上,对方法进行增强.AOP的底层原理就是代理技术(第一种:jdk的动态代理(编写程序必须要有接口).第二种:cglib代理技术(生成类的子类).如果编写的程序有借 ...
- Spring基础(二)_面向切面(AOP)
面向切面编程 面向切面编程[AOP,Aspect Oriented Programming]:通过预编译方式和运行期间动态代理实现程序功能的统一维护的技术.AOP 是 Spring 框架中的一个重要内 ...
- Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)
关于依赖注入, 这篇博文写的非常简单易懂. https://github.com/android-cn/blog/tree/master/java/dependency-injection 此外, 博 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- SpringMVC---依赖注入与面向切面
1.依赖注入与面向切面 1.1.出现背景 ——如何简化java开发? 其中很重要的一点是“组件化”. ——如何更好的“组件化”? 松耦合,以及尽可能的让组件专注于本身. ——Spring框架的目的也只 ...
- Spring5:面向切面
静态代理 缺点:一个真实角色就会产生一个代理角色,代码量会翻倍! 场景:要在写好的实现方法上加入日志功能(公共功能),不要修改原代码 1:原代码 业务接口: package com.spring; p ...
随机推荐
- 通过pyenv进行多版本python管理
1.安装pyenv brew install pyenv 2.配置.zshrc文件 export PYENV_ROOT=/usr/local/var/pyenv if which pyenv > ...
- 自动改变html font-size,实现移动端rem适配
移动端采用rem适配非常方便 比如在iphone6尺寸下,将html font-size 设置为 100px,那么写css时,只要将尺寸/100 + rem 即可. 在iphone6Plus尺寸下,h ...
- 基于HttpClient实现网络爬虫~以百度新闻为例
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791 基于HttpClient4.5实现网络爬虫请訪问这里:http:/ ...
- TSharding源码阅读
需要的背景知识:Spring 和Mybatis 实现原理和源码, javaassist字节码增强的使用, java及设计模式的使用 1 读取解析数据库配置文件 DataSourceScanner实现了 ...
- iOS表格制作
由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...
- Myeclipse 选中高亮
打开显示功能 选择Windows->Preferences->Java-> Editor-> Mark Occurrences ,勾选选项.这时,当你单击一个元素的时候,代码中 ...
- elasticsearch从入门到出门-03-多种搜索
1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight sear ...
- python 捕获异常详细信息
import os import sys import traceback BasePath = os.path.dirname(os.getcwd()) sys.path.append(BasePa ...
- Qt插件开发入门(两种方法:High-Level API接口,Low-Level API接口)
Qt中为我们提供了两种开发插件的方式.一种是使用High-Level API接口,一种是使用Low-Level API接口.所谓High-Level API 是指通过继承Qt为我们提供的特定的插件基类 ...
- go语言之并发编程 channel(1)
单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...