AOP为Aspect Oriented Programming的缩写,意为:面向切面编程

一 前期工作

1.新建一个java项目,我是使用的maven,所以我新建了一个简单的maven项目,因为maven项目可以省去到处找jar包的时间,还是比较方便的

2.引入需要的jar包,我的pom文件中的dependency为

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.16.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.1.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.16.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.16.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.11.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.2.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>

3.新建java文件

①接口,新建一个HelloService的接口

②实现类,新建一个HelloServiceImpl的类,实现上面的接口

③切面,新建一个HelloAspect的类,在aop中就叫做前面

④测试类,新建一个AopTest的测试类

4.在src/java/resource文件夹下面建立一个aopContext.xml的xml文件,schema为

<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

5.将bean资源引入xml

 <bean id="hello" class="com.qiao.aop.HelloServiceImpl"/>
<bean id="aspect" class="com.qiao.aop.HelloAspect" />

二 aop测试开始

1.前置通知

HelloService:  public void beforeSay(String param);

HelloServiceImpl: public void beforeSay(String
param) {

System.out.println(" beforeSay method  param="+param);

}

HelloAspect: public void beforeSayAspect(String
para){

System.out.println(" aspect before param = "+para);

}

aopTest : @Test

public void beforeAspectTest(){

ApplicationContext app = new ClassPathXmlApplicationContext("aopContext.xml");



HelloService hello = (HelloService) app.getBean("hello",HelloService.class);



hello.beforeSay("hello");

}

在xml文件中配置

    <aop:config>
<span style="white-space:pre"> </span> <aop:pointcut expression="<span style="font-family: Arial, Helvetica, sans-serif;">execution(* com.qiao.aop.*.*(..)) and args(param)</span>" id="pointcut"/>
<span style="white-space:pre"> </span> <aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="beforeSayAspect(java.lang.String)" arg-names="param"/>
<span style="white-space:pre"> </span></aop:aspect>
</aop:config>

(<aop:pointcut> 是定义切入点表达式,execution是切入点表达式,第一个*表示任意返回类型,第二个* 表示com.qiao.aop下面的所有的类,第三个*表示类下的所有方法,括号中的“..”表示方法中的任意参数

<aop:aspect> ref指向的是切面,在xml文件中配置的切面的id

<aop:before> 表示这个是前置通知,pointcut-ref指向的是上面pointcut的id,就是使用其中的表达式,也可以在pointcut中直接赋值,即pointcut="execution(* com.qiao.aop.*.*(..)) and args(param)"  在本身直接定义切入点表达式,注意,pointcut和pointcut-ref只能使用一个,两个都使用就会报错。

注意:在aop:pointcut中的args的参数名必须和aop:before中的arg-names的中的参数必须一样,这样,调用切入点是的时候,才会将切入点方法里面的参数传入通知方法中。即。

运行aoptest中的beforeAspectTest,最后输出

在这里面,HelloAspect就是切面,HelloAspect中的方法beforeSayAspect就是通知,HelloService中的方法beforeSay就是切入点。

2.后置返回通知

HelloService:public boolean afterSayReturning();

HelloServiceImpl : public boolean afterSayReturning() {



System.out.println("after return");



return true;

}

HelloAspect:public void afterAspectReturning(Object val){

System.out.println(" after aspect returning val = "+val);

}

AopTest : @Test

public void afterReturningAspectTest(){

ApplicationContext app = new ClassPathXmlApplicationContext("aopContext.xml");



HelloService hello = (HelloService) app.getBean("hello",HelloService.class);

hello.afterSayReturning();

}

xml

		<aop:config>
<span style="white-space:pre"> </span><aop:pointcut expression="execution(* com.qiao.aop.*.*(..))" id="pointcut"/>
<span style="white-space:pre"> </span><aop:aspect ref="aspect">
<aop:after-returning pointcut-ref="pointcut" method="afterAspectReturning" arg-names="val" returning="val"/>
<span style="white-space:pre"> </span> </aop:aspect>
<span style="white-space:pre"> </span> </aop:config>

最后输出

这个我估计实在切入点方法执行之后,将返回值放在后置方法中,然后输出。个人猜测啊。

3.后置异常通知

HelloService
:  public void afterException();

HelloServiceImpl
:   public void afterException() {

System.out.println("after excetion");

throw new RuntimeException();

}

HelloAspect :  public void afterExceptionAspect(Exception e){

System.out.println(" after Exception and e is "+e);

}

AopTest : @Test

public void afterExceptionAspectTest(){

ApplicationContext app = new ClassPathXmlApplicationContext("aopContext.xml");



HelloService hello = (HelloService) app.getBean("hello",HelloService.class);



hello.afterException();

}

xml :

	<aop:config>
<aop:pointcut expression="execution(* com.qiao.aop.*.*(..))" id="pointcut"/>
<span style="white-space:pre"> </span> <aop:aspect ref="aspect">
<aop:after-throwing pointcut-ref="pointcut" method="afterExceptionAspect" arg-names="e" throwing="e"/>
</aop:aspect>
</aop:config>

结果: 

(这个应该是吧通知放到了catch里面)

4.后置最终通知:在切入点选择的方法返回时执行,不管是正常返回还是抛出异常都执行

HelloService
: public void afterFinally();

HelloServiceImpl
: public void afterFinally() {

System.out.println("after finally ");

throw new RuntimeException();

}

HelloAspect : public void afterFinallyAspect(){

System.out.println("after fianlly aspect");

}

AopTest  :  @Test

public void afterFinallyAspectTest(){

ApplicationContext app = new ClassPathXmlApplicationContext("aopContext.xml");



HelloService hello = (HelloService) app.getBean("hello",HelloService.class);



hello.afterFinally();

}

xml :

		<aop:config>
<aop:pointcut expression="execution(* com.qiao.aop.*.*(..))" id="pointcut"/>
<aop:aspect ref="aspect">
<aop:after pointcut-ref="pointcut" method="afterFinallyAspect" />
</aop:aspect>
</aop:config>

(这个难道是放在try catch中的finally里面执行的?)

5.环绕通知:环绕着在切入点选择的连接点处的方法所执行的通知,环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值

HelloService:   public
void sayAround(String param);

HelloServiceImpl : public
void sayAround(String param) {



System.out.println(" around  "+param);



}

HelloAspect : 

public Object aroundAspect(ProceedingJoinPoint pjp) throws Throwable{

System.out.println(" around before ");



Object val = pjp.proceed(new Object[]{"replace"});



System.out.println(" around after ");



return val;

}

AopTest  :   @Test

public void aroundAspectTest(){

ApplicationContext app = new ClassPathXmlApplicationContext("aopContext.xml");



HelloService hello = (HelloService) app.getBean("hello",HelloService.class);



hello.sayAround("haha");

}

xml :

		<aop:config>
<aop:pointcut expression="execution(* com.qiao.aop.*.*(..))" id="pointcut"/>
<aop:aspect ref="aspect">
<aop:around method="aroundAspect" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>

最后输出:

以上只是个人观点,不一定,正确。

最后推荐大神的博客 : http://www.iteye.com/blogs/subjects/spring3?page=2 ,最近在学习大神的博客,可以看看

spring的aop 基于schema的更多相关文章

  1. spring aop 基于schema的aop

    AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP ...

  2. 第三章 AOP 基于Schema的AOP

    基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...

  3. Spring的AOP基于AspectJ的注解方式开发3

    上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...

  4. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

  5. Spring的AOP基于AspectJ的注解方式开发2

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...

  6. Spring的AOP基于AspectJ的注解方式开发1

    参考自黑马培训机构 创建项目,引入jar包 编写目标类,切面类并完成配置 package spring.day2_aop2; /* * 编写目标类 */ public class OrderDao { ...

  7. 开涛spring3(6.3) - AOP 之 6.3 基于Schema的AOP

    6.3  基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...

  8. spring3: 基于Schema的AOP

    6.3  基于Schema的AOP 基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面.切入点及声明通知. 在Spring配置文件中,所以AOP相关定义必须放在<a ...

  9. 基于Schema的AOP 配置使用详解

    原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...

随机推荐

  1. iOS 设备定位功能可用的判断

    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] ...

  2. CF765F Souvenirs 离线+线段树+主席树

    $ \color{#0066ff}{ 题目描述 }$ A县旁,连绵着一条长度为 n 的山脉,这条山脉由 n 座山峰组成,第 i 座山 峰的高度为 ai.作为著名的旅游县城,每天来到山脉游玩的旅客络绎不 ...

  3. ElasticSearch 从2.2升级到6.2.4所碰到的问题汇总

    1.ID的问题. 以前创建索引API直接用URL加索引Post过去就行了,或者在Kibana的开发工具中提交命令 PUT /customer?pretty 但是发现这样即使生成了索引,在ES中预览能看 ...

  4. windows下webpack不是内部命令 解决方法

    安装webpack 到打包文件一路出现的各种问题 windows下webpack不是内部命令 安装完webpack后要加下环境变量 系统变量新建 NODE_PATH 变量值E:\demo\webpac ...

  5. 自动更新R软件

    利用R语言命令自动更新R语言软件的版本. install.packages("installr") library(installr) updateR()

  6. 2019 CCPC-Wannafly Winter Camp Day2(Div2, onsite)

    solve 4/11 A Erase Numbers II Code:KK Thinking :KK 用ans表示当前最优答案,maxx表示遍历到的最大数字,一开始ans肯定等于a[ 1 ]+a[ 2 ...

  7. nginx(四)-负载均衡

    负载均衡,我认为是nginx最重要的功能了.那什么是负载均衡呢. 比如有一个服务,它访问量很大,一台机器吃不消了,怎么办,我们准备两台.分一部分的请求出来.现在有两台服务器提供这个服务.我们访问其中一 ...

  8. ios真机测试问题

    前端页面在ios端真机测试出现的问题 由于苹果对于性能的要求是近乎苛刻,如果没有可点的特性的元素系统默认不会给它响应事件,因此真机测试时容易添加不上绑定事件 解决办法: 1.通过js判断当前是否为苹果 ...

  9. MVC返回数据流,ajax接受并保存文件

    js代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. android 报错: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHelper;

    在使用SmartRefreshLayout时,报 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/a ...