具体案例放在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. 【转】c# winform 打包部署 自定义界面 或设置开机启动

    方法一: 创建安装部署这部分就不用说了,添加安装部署项目后,鼠标右键安装项目->视图->注册表, 要使软件在开机就运行,可以在HKEY_CURRENT_USER\Software\Micr ...

  2. 火狐和ie下获取javascript 获取event

    javascript 获取event 先从一个简单的例子说起,一个简单的button控件如下: <input type='button' name='mybtn' id='mybtn' oncl ...

  3. jquery技巧总结

    jquery技巧总结一.简介 1.1.概述随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery.mootool ...

  4. DataList与Repeater嵌套绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs&quo ...

  5. com.opensymphony.module.sitemesh.filter.PageFilter 装饰页面

    1.web.xml中配置: <filter> <filter-name>sitemeshFilter</filter-name> <filter-class& ...

  6. windows下脚本配置IP地址

    带着笔记本有时候在固定的地方工作,需要用到同一个的Ip地址.换个地方换个Ip,又要重新输一遍. 开始感觉这个过程很繁琐,因为是window工作环境,一开始想到了vbs脚本. 无意中发现了强大的nets ...

  7. [转]谈谈select, iocp, epoll,kqueue及各种网络I/O复用机制

    参考原文:再谈select, iocp, epoll,kqueue及各种I/O复用机制 一.I/O模型概述 介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblocking ...

  8. Angular学习(2)- ng-app

    此例子看alert弹出时的效果.当然,最重要的是ng-app="MyApp",这一个是怎么加载的. <!DOCTYPE html> <html ng-app=&q ...

  9. linux杂谈

    1. 目录的stick位 一般情况下,如果一个用户对一个目录有写权限,那么他就可以删除该目录下的文件,即使这些文件不是他的.为了防止这种情况,我们需要为目录设置stick位: chmod a+t yo ...

  10. leptus和cowboy之间某些库的转换

    在github上问作者,如何使用cookie,作者回复是,leptus是基于restful的框架,对于cookie和session的支持,建议转换为cowboy用,以下是转换方式 leptus_req ...