Spring实现代理机制

上一笔记,介绍了代理机制,这一节介绍Spring是如何实现代理。

通过一个例子来说明。

包下载地址(两个都要下):

http://www.blogjava.net/Files/ducklyl/springaop.rar

http://www.blogjava.net/Files/ducklyl/Spring.rar

(1)创建LogBeforeAdvice类(实现MethodBeforeAdvice接口,会在目标对象的方法执行之前被中呼叫)package com.proxy;import java.lang.reflect.*;import java.util.logging.Logger;import java.util.logging.Level;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,"mehtod starts "+method);}}

(2)创建配置文件advice-config.xmlclass="org.springframework.aop.framework.ProxyFactoryBean">com.proxy.IHellologBeforeAdvice(3)测试类SpringAOPDemopackage com.proxy;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class SpringAOPDemo {public static void main(String[] args){//读取配置文件 ApplicationContext context=new FileSystemXmlApplicationContext("advice-config.xml");IHello helloProxy=(IHello)context.getBean("helloProxy");helloProxy.hello("ducklyl");}}

运行测试类,结果如下:

Hello,ducklyl

离开之后,我想你不要忘记一件事:不要忘记想念我。

Spring实现代理机制

相关文章:

你感兴趣的文章:

标签云: