具体案例放在github上,主要是jar包在上面

https://github.com/guoyansi/spring-aop-example

knights.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="knight" class="p1.BraveKnight">
<constructor-arg ref="quest" />
</bean>
<bean id="quest" class="p1.SlayDragonQuest"></bean> <bean id="minstrel" class="p1.Minstrel"></bean> <aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" />
<aop:before pointcut-ref="embark" method="singBeforeQuest"/>
<aop:after pointcut-ref="embark" method="singAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>

IQuest.java

package p1;

/**
* 探险
*
*/
public interface IQuest {
void embark();
}

IKnight.java

package p1;

public interface IKnight {
void embarkOnQuest();
}

BraveKnight.java

package p1;

/**
* 骑士
*
*/
public class BraveKnight implements IKnight{
private IQuest quest; public BraveKnight(IQuest quest){
this.quest=quest; }
@Override
public void embarkOnQuest (){
quest.embark();
} }

SlayDragonQuest.java

package p1;

public class SlayDragonQuest implements IQuest{
@Override
public void embark() {
System.out.println("SlayDragonQuest的embark......");
}
}

Minstrel.java

package p1;

public class Minstrel {
public void singBeforeQuest(){
System.out.println("探险之前...");
}
public void singAfterQuest(){
System.out.println("探险之后......");
} }
package p1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Run {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:knights.xml");
IKnight knight=(IKnight)context.getBean("knight");
knight.embarkOnQuest();
}
}

执行run的结果:

上面是一个完整的例子.如果没有commons-logging.jar

控制台上的那些红色字样就不会输出,还会出现异常.

如果没有aopalliance.jar:

nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

如果没有aspectjweaver.jar

nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

spring-aop示例的更多相关文章

  1. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  2. Spring AOP示例代码

    public interface CustomerDao { public void save(); public void update(); } public class CustomerDaoI ...

  3. Spring AOP 知识整理

    通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...

  4. Spring AOP学习笔记01:AOP概述

    1. AOP概述 软件开发一直在寻求更加高效.更易维护甚至更易扩展的方式.为了提高开发效率,我们对开发使用的语言进行抽象,走过了从汇编时代到现在各种高级语言繁盛之时期:为了便于维护和扩展,我们对某些相 ...

  5. Spring aop 简单示例

    简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...

  6. 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~

    Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...

  7. Spring AOP的简单示例

    配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...

  8. Spring AOP源码分析(一)使用示例

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 我们知道,使用面向对象编程(OOP)有一些弊端,当需要为多个不具有继承 ...

  9. 在Intellij上面导入项目 & AOP示例项目 & AspectJ学习 & Spring AoP学习

    为了学习这篇文章里面下载的代码:http://www.cnblogs.com/charlesblc/p/6083687.html 需要用Intellij导入一个已有工程.源文件原始内容也可见:link ...

  10. spring aop介绍和示例

    参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...

随机推荐

  1. JSP 相关试题(三)

    判断题 1.动态网页和静态网页的根本区别在于服务器端返回的HTML文件是事先存储好的还是由动态网页程序生成的(对 ) 2.Web开发技术包括客户端和服务器端的技术.(对 ) 3.Tomcat和JDK都 ...

  2. mfs-用户

    http://blog.csdn.net/liuyunfengheda/article/details/5260278 MFS总结 http://bbs.chinaunix.net/thread-16 ...

  3. linux包之包管理命令rpm-yum

    背景 YUM(Yellow dog Updater, Modified)为多个Linux发行版的前端软件包管理器,例如 Redhat RHEL, CentOS & Fedora. YUM通过调 ...

  4. 微软发布了ASP.NET WebHooks预览版

    微软 近期发布了ASP.NET WebHooks的预览版 ,这是一个可用于创建及使用Webhook功能的库.WebHooks支持MVC 5及WebApi 2. Webhook是一种通过HTTP实现用户 ...

  5. MongoDB 入门与实例

    一.准备工作 1. 下载mongoDB 下载地址:http://www.mongodb.org/downloads 选择合适你的版本 相关文档:http://www.mongodb.org/displ ...

  6. mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法

    权限问题,授权 给 root  所有sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  7. Hive(一):架构及知识体系

    Hive是一个基于Hadoop的数据仓库,最初由Facebook提供,使用HQL作为查询接口.HDFS作为存储底层.mapReduce作为执行层,设计目的是让SQL技能良好,但Java技能较弱的分析师 ...

  8. C#分析搜索引擎URL得到搜索关键字,并判断页面停留时间以及来源页面

    前台代码: var start; var end; var state; var lasturl = document.referrer; start = new Date($.ajax({ asyn ...

  9. Python 通过pickle标准库加载和保存数据对象

    import pickle with open('mydata.pickle','wb') as mysavedata: pickle.dump([1,2,'three'], mysavedata) ...

  10. AWS控制台改英文

    https://console.amazonaws.cn 控制台首选项->语言->英文