spring-aop示例
具体案例放在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示例的更多相关文章
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- Spring AOP示例代码
public interface CustomerDao { public void save(); public void update(); } public class CustomerDaoI ...
- Spring AOP 知识整理
通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...
- Spring AOP学习笔记01:AOP概述
1. AOP概述 软件开发一直在寻求更加高效.更易维护甚至更易扩展的方式.为了提高开发效率,我们对开发使用的语言进行抽象,走过了从汇编时代到现在各种高级语言繁盛之时期:为了便于维护和扩展,我们对某些相 ...
- Spring aop 简单示例
简单的记录一下spring aop的一个示例 基于两种配置方式: 基于xml配置 基于注解配置 这个例子是模拟对数据库的更改操作添加事物 其实并没有添加,只是简单的输出了一下记录 首先看下整个例子的目 ...
- 用心整理 | Spring AOP 干货文章,图文并茂,附带 AOP 示例 ~
Spring AOP 是 Java 面试的必考点,我们需要了解 AOP 的基本概念及原理.那么 Spring AOP 到底是啥,为什么面试官这么喜欢问它呢?本文先介绍 AOP 的基本概念,然后根据 A ...
- Spring AOP的简单示例
配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...
- Spring AOP源码分析(一)使用示例
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 我们知道,使用面向对象编程(OOP)有一些弊端,当需要为多个不具有继承 ...
- 在Intellij上面导入项目 & AOP示例项目 & AspectJ学习 & Spring AoP学习
为了学习这篇文章里面下载的代码:http://www.cnblogs.com/charlesblc/p/6083687.html 需要用Intellij导入一个已有工程.源文件原始内容也可见:link ...
- spring aop介绍和示例
参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...
随机推荐
- SiteMesh详解
Sitemesh是一种页面装饰技术:它通过过滤器(filter)来拦截页面访问,据被访问页面的URL找到合适的装饰模板等等,感兴趣的朋友可以了解下哦 一,基本概念 1,Sitemesh是一种页面装饰技 ...
- 如何利用RMAN Debug和10046 Trace来诊断RMAN问题?
学习转摘:https://blogs.oracle.com/Database4CN/entry/%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8rman_debug%E5%92 ...
- [oracle] ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
ORACLE 32位数据库正常安装,sqlplus 正常连接数据库但是PL/SQL developer 64位却报出这个错误. 第一反应是缺少32位客户端.下载安装,配置完成后如图所示: 还是报这个错 ...
- Python基础教程【读书笔记】 - 2016/7/5
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第三波:第8章 异常 [总览]学习如何创建和引发自定义的异常,以及处理异常的各种方法. 为了能够处理异常事件,可以再所 ...
- php 自带函数
memory_get_usage()://查看当前内存使用情况单位 bytes str_repeat("liuhui", 2);//字符串重复指定次数,liuhui重复2次
- .net下MD5算法和加盐
MD5方法: public static string GetMD5(string sDataIn) { MD5CryptoServiceProvider md5 ...
- 【solr】solr5.0整合中文分词器
1.solr自带的分词器远远满足不了中文分词的需求,经查使用最多的分词器是solr是mmseg4j分词器,具体整合大家可以参考 https://github.com/zhuomingliang/mms ...
- JavaScript-获得当前时间
js获得当前时间 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位, ...
- java 对象传递 是 值传递 还是 引用传递?
这个问题说实话我感觉没有太大的意义. 按第一印象和c++的一些思想去理解的话对象传递是引用传递,因为传递过去的对象的值能被改变. 但是又有很多人,不知道从哪里扣出来一句,java中只有值传递,没有引用 ...
- ExtJs4 SpringMvc3 实现Grid 分页
新建一个Maven webapp项目,webxml以及spring配置没什么需要注意的,不再赘述. Maven依赖:(个人习惯,有用没用的都加上...) <project xmlns=" ...