- 浏览: 107409 次
- 性别:
- 来自: 北京
最新评论
-
nwpu7:
解决问题,感谢感谢
org.hibernate.hql.ast.HqlToken 错误weblogic异常 -
zhangna307:
如果已经有对应的hibernte-cfg.xml文件,还是报这 ...
Could not instantiate cache implementation异常处理 -
zzw118:
不错,解决了我的问题!
Could not instantiate cache implementation异常处理 -
flank.tai:
女孩的技术都这么牛!!顶。不过图片看不了呀!!!
深度技术 USB启动维护盘制作工具 -
supercode:
dell的服务器与自己diy的相同配置,价格高出多少左右
Dell PowerEdge 2800 服务器 Windows 2003安装手册
一,从代理机制初探AOP
1,使用静态代理
#代理对象和被代理对象必须实现共同的接口 package com.biaoflying; public interface IHello { public void hello(String name); } |
#代理对象HelloProxy package com.biaoflying; import java.util.logging.*; public class HelloProxy implements IHello { private IHello helloObject; private Logger logger= Logger.getLogger(this.getClass().getName()); public HelloProxy(IHello helloObject){ this.helloObject=helloObject; } public void hello(String name){ log("hello method starts...."); helloObject.hello(name); log("hello method ends...."); } private void log(String msg){ logger.log(Level.INFO,msg); } } |
#HelloSpeaker被代理对象 package com.biaoflying; public class HelloSpeaker implements IHello { public void hello(String name){ System.out.println("Hello "+name); } } |
#主程序 package com.biaoflying; public class ProxyDemo { public static void main(String[] args){ HelloProxy proxy= new HelloProxy(new HelloSpeaker()); proxy.hello("biaoflying"); } } |
#执行ProxyDemo的输出 信息: hello method starts.... Hello biaoflying 2008-3-21 9:05:40 com.biaoflying.HelloProxy log 信息: hello method ends.... |
TIP:使用静态代理,代理对象和被代理对象必须实现共同的接口。本质上就是由代理对象呼叫被代理对象并执行相关的逻辑。同时可以看到,静态代理在程序规模稍大时就无法胜任了。
-----------------------------------------------------------------------------------
2,动态代理
去掉HelloProxy.java,编写以下的LogHandler.java文件。
package com.biaoflying; import java.util.logging.*; import java.lang.reflect.*; public class LogHandler implements InvocationHandler{ private Logger logger= Logger.getLogger(this.getClass().getName()); private Object delegate; public Object bind(Object delegate){ this.delegate=delegate; return Proxy.newProxyInstance( delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this); } public Object invoke(Object proxy,Method method, Object[] args)throws Throwable{ Object result = null; try{ log("method starts..."+method); result=method.invoke(delegate,args); logger.log(Level.INFO,"method ends..."+method); }catch(Exception e){ log(e.toString()); } return result; } private void log(String msg){ logger.log(Level.INFO,msg); } } |
#主程序 package com.biaoflying; public class ProxyDemo2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LogHandler logHandler=new LogHandler(); IHello helloProxy= (IHello)logHandler.bind(new HelloSpeaker()); helloProxy.hello("biaoflying"); } } |
TIP:动态代理的执行流程是Proxy.newProxyInstance(..,..,this)方法返回一个代理对象,该代理对象每次执行相关的方法时都会委托给this.invoke方法。
-----------------------------------------------------------------------------------
二,Advices
由于织入Targets的时机不同,Advices被分为以下几种:Before advice,After advice,Around advice,Throw advice。
#MethodBeforeAdvice接口 package org.springframework.aop; public interface MethodBeforeAdvice extends BeforeAdvice{ void before(Method method,Object[] args,Object target) throws Throwable; } |
具体的例子,IHello接口和HelloSpeaker.java如上
package com.biaoflying; import java.lang.reflect.Method; import java.util.logging.*; import org.springframework.aop.MethodBeforeAdvice; public class LogBeforeAdvice implements MethodBeforeAdvice{ private Logger logger= Logger.getLogger(this.getClass().getName()); public void before(Method method,Object[] args, Object target)throws Throwable{ logger.log(Level.INFO,"Method starts..."+method); } } |
package com.biaoflying; import org.springframework.context.*; import org.springframework.context.support.*; public class SpringAOPDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config.xml"); IHello helloProxy=(IHello)context.getBean( "helloProxy"); helloProxy.hello("biaoflying"); } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="logBeforeAdvice" class="com.biaoflying.LogBeforeAdvice"/> <bean id="helloSpeaker" class="com.biaoflying.HelloSpeaker"/> <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.biaoflying.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>logBeforeAdvice</value> </list> </property> </bean> </beans> |
执行SpringAOPDemo.java会在hello方法执行之前输出log消息。
另外对于After advice,Around advice是类似的。
-----------------------------------------------------------------------------------
三,Throw Advice
表示在目标对象调用方法发生异常时添加一个Aspect。
package com.biaoflying; public interface IHello { public void hello(String name)throws Throwable; } |
package com.biaoflying; public class HelloSpeaker implements IHello { public void hello(String name)throws Throwable{ System.out.println("Hello "+name); throw new Exception("发生异常..."); } } |
package com.biaoflying; import java.lang.reflect.Method; import java.util.logging.*; import org.springframework.aop.ThrowsAdvice; public class SomeThrowAdvice implements ThrowsAdvice{ private Logger logger= Logger.getLogger(this.getClass().getName()); public void afterThrowing(Method method,Object[] args,Object target,Throwable subclass){ logger.log(Level.INFO, "Logging that a "+subclass+ "Exception was thrown in "+method); } } |
package com.biaoflying; import org.springframework.context.ApplicationContext; import org.springframework. context.support.FileSystemXmlApplicationContext; public class SpringAOPDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config.xml"); IHello helloProxy= (IHello)context.getBean("helloProxy"); try{ helloProxy.hello("biaoflying"); }catch(Throwable throwable){ System.err.println(throwable); } } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="someThrowAdvice" class="com.biaoflying.SomeThrowAdvice"/> <bean id="helloSpeaker" class="com.biaoflying.HelloSpeaker"/> <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.biaoflying.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>someThrowAdvice</value> </list> </property> </bean> </beans> |
四,更为细化的AOP programming。
通过定义Ponitcut来实现更为细化的AOP编程,如可以指定在Target的某个方法执行时加入advice。
package com.biaoflying; public interface IHello { public void helloNewbie(String name); public void helloMaster(String name); } |
package com.biaoflying; public class HelloSpeaker implements IHello { public void helloNewbie(String name){ System.out.println("Hello "+name+" newbie"); } public void helloMaster(String name){ System.out.println("Hello "+name+" will be master"); } } |
package com.biaoflying; import java.lang.reflect.Method; import java.util.logging.*; import org.springframework.aop.MethodBeforeAdvice; public class LogBeforeAdvice implements MethodBeforeAdvice{ private Logger logger= Logger.getLogger(this.getClass().getName()); public void before(Method method,Object[] args, Object target)throws Throwable{ logger.log(Level.INFO,"Method starts..."+method); } } |
package com.biaoflying; import org.springframework.context.ApplicationContext; import org.springframework. context.support.FileSystemXmlApplicationContext; public class SpringAOPDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config1.xml"); IHello helloProxy= (IHello)context.getBean("helloProxy"); helloProxy.helloNewbie("abio"); helloProxy.helloMaster("biaoflying"); } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="logBeforeAdvice" class="com.biaoflying.LogBeforeAdvice"/> <bean id="helloAdvisor" class="org.springframework.aop. support.NameMatchMethodPointcutAdvisor"> <property name="mappedName"> <value>hello*</value> </property> <property name="advice"> <ref bean="logBeforeAdvice"/> </property> </bean> <bean id="helloSpeaker" class="com. biaoflying.HelloSpeaker"/> <bean id="helloProxy" class="org.springframework. aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.biaoflying.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>helloAdvisor</value> </list> </property> </bean> </beans> |
另外一个静态PointcutAdvisor为RegexpMethodPointcutAdvisor,更改以上的配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="logBeforeAdvice" class="com.biaoflying.LogBeforeAdvice"/> <bean id="helloAdvisor" class="org.springframework.aop. support.RegexpMethodPointcutAdvisor"> <property name="pattern"> <value>.*hello.*</value> </property> <property name="advice"> <ref bean="logBeforeAdvice"/> </property> </bean> <bean id="helloSpeaker" class="com.biaoflying. HelloSpeaker"/> <bean id="helloProxy" class="org.springframework. aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.biaoflying.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>helloAdvisor</value> </list> </property> </bean> </beans> |
两者都可以得到一下的输出:
信息: Method starts...public abstract void com.biaoflying.IHello.helloNewbie(java.lang.String) 2008-3-22 18:38:22 com.biaoflying.LogBeforeAdvice before 信息: Method starts...public abstract void com.biaoflying.IHello.helloMaster(java.lang.String) Hello abio newbie Hello biaoflying will be master |
发表评论
-
求100以内的质数
2011-01-11 12:42 902题目:除了能被自身和1整除外,不能被其他数整除的数,为素数 ... -
整理开始
2010-03-15 16:49 770将近半年没来javaeye了,自己的知识没能及时梳理。从今天开 ... -
dddd
2009-09-16 17:11 795ddddddddd -
java文件上传下载
2009-09-16 17:07 5502文件上传在web应用中非常普遍,要在jsp环境中实现文件上传功 ... -
实现Ext表单对checkBoxGroup的统一管理
2009-08-11 13:21 13511 对于类型是checkboxgroup的数据,数据库中保存数 ... -
cvsnt 配置
2009-06-03 15:51 1086CvsNT设置1.安装CvsNT,并 ... -
发生算术溢出或其它算术异常。
2009-05-26 17:16 3686今天报了一个bug,sql在DB2执行的时候报“发生算术溢出或 ... -
回车代替点按钮提交
2009-05-22 16:46 1493输入框+按钮 完成的操作和 输入框+回车 完成一样的操作 & ... -
eclipse下修改代码重启tomcat的问题
2009-05-04 17:40 2255忍受了很久的eclipse下修改代码重启tomcat的问题,今 ... -
urlrewritefilter 例子
2009-04-17 17:31 1135我在tomcat下跑通了一个例子,关于urlRewritefi ... -
正则表达式
2009-03-20 17:35 820如果你曾经用过Perl或任何其他内建正则表达式支持的语言 ... -
正则表达式大全
2009-03-20 16:35 875常用正则表达式大全! ... -
Could not instantiate cache implementation异常处理
2009-03-06 14:34 7966今天遇到了一个很奇怪的问题 我照例增加了一套新功能,但一切就 ... -
整理分页
2009-02-24 16:28 846从今天开始打算整理一下分页、上传下载、程序里调用存储 ... -
Dell PowerEdge 2800 服务器 Windows 2003安装手册
2009-02-06 15:40 2218Dell PowerEdge 2800 服务器 W ... -
新年第一天的工作
2009-02-05 11:50 723初十一来上班了,工作安排是解决河北bug,但是公司服务器dow ... -
深度技术 USB启动维护盘制作工具
2009-02-05 11:27 5907前言 大容量的移动硬盘和U盘已经越来越普及,很多朋友希望将 ... -
Tomcat:IOException while loading persisted session
2009-01-05 10:20 854错误描述:....while loading persiste ... -
display tag
2008-12-29 10:19 4273DisplayTag Tutorial by DarrenWa ... -
eclipse快捷键大全
2008-12-17 17:11 583Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl ...
相关推荐
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
spring aop jar 包
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
springAOP配置实现动态代理,有利于熟悉动态代理原理,深入了解spring。
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
spring aop的demo spring aop的demo
Spring AOP 入门 作者:廖雪峰
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
Spring AOP简单demo 入门级的 advice
springAOP详解
spring aop 经典例子(原创),无论新手还是老手,可以快速对Spring AOP有个总体的认识。基于JDK1.6,Spring2.0.5,Eclipse IDE。
springaop多数据库读写分离