在我们的spring xml配置中,加上<aop:config>之后,Eclipse报错,提示The prefix "aop" for element "aop:config" is not bound,这是因为,需要在beans中加入xmlns:aop="http://www.springframework.org/schema/aop",以及在xsi:schemaLocation中加入http://www.springframework.org/schema/aop和http://www.springframework.org/schema/aop/spring-aop.xsd。然后问题就可以解决了。

  再运行程序,报错Unexpected exception parsing XML document from class path resource [com/juggler/juggler.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice。这是因为缺少了一个aopalliance.jar,下载了一个aopalliance-1.0.jar之后。

  运行后报bean创建异常了异常信息是这样的:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'juggler22' defined in class path resource [com/juggler/juggler.xml]:

BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0':

Cannot create inner bean '(inner bean)#5f0ab09f' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument;

nested exception is org.springframework.beans.factory.BeanCreationException:

Error creating bean with name '(inner bean)#5f0ab09f': Cannot create inner bean '(inner bean)#6c69d02b' of type [org.springframework.aop.aspectj.AspectJExpressionPointcut] while setting constructor argument;

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#6c69d02b':

Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

  错误消息很长,意思就是在创建我们自己指定的juggler bean的时候,内部对象发生错误了。一开始以为是自己的juggler bean定义的时候有问题,不过之前也是这么定义的,所以应该没问题,删除掉aop的config部分之后,程序也能正常运行。那么就是string aop部分引入的问题了,google后发现,其实又是少了aspectj-1.6.9.jar包,还有aspectjweaver-1.6.9.jar包。因为spring的aop是使用了aspectj的部分功能的,所以需要引入aspectj的jar包也是正常。

  不过这里不得不吐槽一下java的第三方包库。。。真的有点环环相扣的感觉了,部署环境比较恶心。

  不过这个问题解决了之后,我的第一次spring aop的简单测试,也是顺利的输出预计的结果了。

  其中Juggler类:

package com.juggler;

public class Juggler {
private int beanBags = 0;
private Poem poem = null;
private Instructment instructment; public Juggler(int _beanBags, Poem _poem) {
System.out.println("Juggler constructor");
beanBags = _beanBags;
poem = _poem;
} public void perform() {
System.out.println("throw " + Integer.toString(beanBags) + " _beanBags");
poem.read();
instructment.play();
} public void initObject() {
System.out.println("juggler initObject calls");
} public void destroyObject() {
System.out.println("juggler destroyObject calls");
} public void setInstructment(Instructment _instructment) {
instructment = _instructment;
}
}

  Audience类代码:

package com.juggler;

public class Audience {
public Audience() { }
public void takeSeat() {
System.out.println("audience take seat");
} public void applaud() {
System.out.println("clap clap clap");
}
}

  juggler.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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/tx
http://www.springframework.org/schema/tx/spring-tx.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"
default-init-method = "initObject"
default-destroy-method = "destroyObject"> <bean id="juggler" class="com.juggler.Juggler">
<constructor-arg value="5" />
<constructor-arg ref="sonnet" />
<property name="instructment" ref="piano" />
</bean> <bean id="sonnet" class="com.juggler.SonnetPoem"/>
<bean id="sonnetMulti" class="com.juggler.SonnetPoem" scope="prototype"/> <bean id="piano" class="com.juggler.Piano" />
<bean id="audience" class="com.juggler.Audience" /> <aop:config>
<aop:aspect ref="audience">
<aop:before pointcut="execution(* com.juggler.Juggler.perform(..))" method="takeSeat" />
<aop:after pointcut="execution(* com.juggler.Juggler.perform(..))" method="applaud" />
</aop:aspect>
</aop:config>
</beans>

  上述代码,我们有几个类的代码就没有贴出来了,只是简单的测试一下aop的切片功能。

  从xml中我们可以了解,主要是针对juggler的perform做了一个切片,在它执行前,执行后分别调用了我们指定的method。

Spring使用——切面编程AOP使用的更多相关文章

  1. Spring面向切面编程(AOP)

    1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...

  2. Spring面向切面编程(AOP,Aspect Oriented Programming)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  3. Spring面向切面编程(AOP)方式二

    使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...

  4. Spring面向切面编程AOP(around)实战

    spring aop的环绕通知around功能强大,我们这里就不细说,直接上代码,看着注释就能明白 需要的可以点击下载源码 1.如果使用注解的方式则需要先创建个注解类 package com.mb.a ...

  5. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  6. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  7. spring框架(2)— 面相切面编程AOP

    spring框架(2)— 面相切面编程AOP AOP(Aspect Oriented Programming),即面向切面编程. 可以说是OOP(Object Oriented Programming ...

  8. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  9. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

随机推荐

  1. R----dplyr包介绍学习

    dplyr包:plyr包的替代者,专门面对数据框,将ddplyr转变为更易用的接口 %>%来自dplyr包的管道函数,其作用是将前一步的结果直接传参给下一步的函数,从而省略了中间的赋值步骤,可以 ...

  2. 11-JS基础

    JS声明变量 变量必须以字母开头 变量也能以 $ 和 _ 符号开头(不推荐这么做) 变量名称对大小写敏感(y 和 Y 是不同的变量) **提示:JavaScript 语句和 JavaScript 变量 ...

  3. Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式

    这里对linux 的几个命令整理下,有:休眠,挂起,待机,关机等几个命令的区别及如何实现. 休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将 ...

  4. 继续Kanzi

    转眼间,Kanzi已经发展到3.3版本了,之前研究过的东西,今天有空下了个版本跟进更新看看有没有什么变化.新的引擎跟以前2.x版本有很大的差别.新引擎增加了很多新功能(包括局部刷新技术),也跟随大潮加 ...

  5. 2013-6-2 [转载自CSDN]如何入门Windows系统下驱动开发

    [序言]很多人都对驱动开发有兴趣,但往往找不到正确的学习方式.当然这跟驱动开发的本土化资料少有关系.大多学的驱动开发资料都以英文为主,这样让很多驱动初学者很头疼.本人从事驱动开发时间不长也不短,大概 ...

  6. 一步一步学习underscore的封装和扩展方式

    前言 underscore虽然有点过时,这些年要慢慢被Lodash给淘汰或合并. 但通过看它的源码,还是能学到一个库的封装和扩展方式. 第一步,不污染全局环境. ES5中的JS作用域是函数作用域. 函 ...

  7. sizeof既是关键字,又是运算符(操作符),但不是函数!

    sizeof是关键字吗 sizeof是关键字,这一点毋庸置疑.你不能将sizeof定义为任何标识符.查看C语言标准文档里的说明: sizeof是运算符(操作符)吗 C语言中,sizeof是运算符(操作 ...

  8. 20160928_CentOS6.4x64_Oracle11gR2x64

    1.“Oracle Database 11g Release 2” 下载地址: http://www.oracle.com/technetwork/database/enterprise-editio ...

  9. SG函数 专题练习

    [hdu1536][poj2960]S-Nim 题意 题意就是给出一个数组h,为每次可以取石子的数目. 然后给你n堆石子每堆si.求解先手能不能赢? 分析 根据\(h\)数组预处理出\(sg[i]\) ...

  10. iOS 视图控制器转场详解

    iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...