1. 创建通知:定义一个接口

Public interface Sleepable

{

voidsleep(); 

}
然后写一个Human类,他实现了这个接口 publicHuman implements Sleepable{ public void sleep() { System.out.println("睡觉中...!"); } }

  

2.编写一个SleepHelper类,它里面包含了睡觉的辅助工作,用AOP术语来说它就应该是通知

Public class Sleep Helper implements MethodBeforeAdvice,AfterReturningAdvice  {

publicvoidbefore(Method method, Object[] arguments, Object target)throws

Throwable {
System.out.println("睡觉前");
}
publicvoidafterReturning(Object rturnValue, Method method, Object[] arguments, Object target)throwsThrowable {
System.out.println("睡觉后");
}
}

  

然后在spring配置文件中进行配置:<!-- 被代理目标对象 -->

<bean id="Human"class="cn.happy.dao.Human"></bean>

<bean id="SleepHelper"class="cn.happy.aop.SleepHelper"></bean>//定义切点的常用的两种方式:1)使用正则表达式 2)使用AspectJ表达式,//这里用正则表达式<!-- 顾问 -->   
<bean id="BeforAdvisor"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="BeforAdvice"></property> <property name="pattern" value=".*l.*g.*">
</property>
</bean><!-- 代理对象 -->//ProxyFactoryBean是一个代理,我们可以把它转换为//proxyInterfaces中指定的实现该interface的代理对象
<bean id="serviceProxy"class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="Human"></property>
<property name="interceptorNames" value="BeforAdvisor">
</property> </bean>代理类
publicclassStuTest {publicstaticvoidmain(String[] args)
{//通过ClassPathXmlApplicationContext实例化Spring的上下文
ApplicationContext context=newClassPathXmlApplicationContext("applicationContext.xml");
//通过ApplicationContext的getBean()方法,根据id来获取Bean的实例
Sleepable s=(Sleepable)context.getBean("Human");
s.sleep();
}
}

  

Spring aop(实验写法)的更多相关文章

  1. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  2. 基于@AspectJ配置Spring AOP之一--转

    原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...

  3. Spring AOP 四大通知

    Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现  MethodBeforeAdvice 接口,重写 public  void  before(Method  metho ...

  4. Spring AOP中pointcut expression表达式解析 及匹配多个条件

    Spring中事务控制相关配置: <bean id="txManager" class="org.springframework.jdbc.datasource.D ...

  5. spring ioc 原理 spring aop原理

    大家一直都说spring的IOC如何如何的强大,其实我倒觉得不是IOC如何的强大,说白了IOC其实也非常的简单.我们先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对 ...

  6. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  7. jdk动态代理与cglib代理、spring aop代理实现原理

    原创声明:本博客来源与本人另一博客[http://blog.csdn.net/liaohaojian/article/details/63683317]原创作品,绝非他处摘取 代理(proxy)的定义 ...

  8. jdk动态代理与cglib代理、spring aop代理实现原理解析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  9. 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  10. Spring AOP四种实现方式Demo详解与相关知识探究

    一.前言 在网络上看到一篇博客Spring实现AOP的4种方式,博主写的很通俗易懂,但排版实在抓狂,对于我这么一个对排版.代码格式有强迫症的人来说,实在是不能忍受~~~~(>_<)~~~~ ...

随机推荐

  1. Linux思维导图之rpm、yum、编译

    yum安装失败: 1.yum client 路径指向不正确:2.yum server 缓存未清理(yum clean all;yum makecache):3.网络不连通

  2. OpenCV实现USM锐化与测试

    OpenCV实现USM锐化 [转]http://www.programdevelop.com/4964391/ USM (Unsharp masking) is a common operation ...

  3. 怎么样调整FreeBSD时区问题

    一般我们在安装系统的时候,都会遇到服务器时间不同步的情况.所以必须得设置为中国时区,比较简单的方法,就总结如下几点: 1.通过命令行启动图形界面更改 #sysinstall 请选择 configure ...

  4. 数位dp无前导零

    题目链接:http://    www.lydsy.com/JudgeOnline/problem.php?id=1026 #include <iostream> #include < ...

  5. ACDream - k-GCD

    先上题目: B - k-GCD Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...

  6. POJ 1678

    博弈题,使用DP来完成.开始时,我以为可以用极大极小加剪枝可以过,但,TLE... 看过一些题解,没看懂,但也由此有了启发: 我们只记录差(初始为0),那为1选的数即为在原差值上加上该数,2选即是减去 ...

  7. iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?

    iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...

  8. LeetCode: Word Ladder [126]

    [题目] Given two words (start and end), and a dictionary, find the length of shortest transformation s ...

  9. linux下Oracle11g RAC搭建(七)

    linux下Oracle11g RAC搭建(七) 六.安装Grid 启动GRID安装界面 方式一:redhat下调整分辨率 [root@node1 ~]# xhost +       //授权 [ro ...

  10. 【转】HDFS读写流程

    概述开始之前先看看其基本属性,HDFS(Hadoop Distributed File System)是GFS的开源实现. 特点如下: 能够运行在廉价机器上,硬件出错常态,需要具备高容错性流式数据访问 ...