![](/icons/73505yi.gif)
![](/icons/73505dou.gif)
![](/icons/73505dou.gif)
![](/icons/73505yi.gif)
![](/icons/73505dou2.gif)
通过
![](/icons/73505yi.gif)
![](/icons/73505dou2.gif)
包下载地址(两个都要下):
http://www.blogjava.net/Files/ducklyl/springaop.rar
http://www.blogjava.net/Files/ducklyl/Spring.rar
(1)创建LogBeforeAdvice类(实现MethodBeforeAdvice接口
![](/icons/73505dou.gif)
![](/icons/73505de.gif)
package com.proxy;
import java.lang.reflect.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.springframework.aop.MethodBeforeAdvice;;
public
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.xml<?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"
="com.proxy.LogBeforeAdvice" />
<bean id="helloSpeaker"
="com.proxy.HelloSpeaker" />
<bean id="helloProxy"
="org.springframework.aop.framework.ProxyFactoryBean"><!--建立代理对象-->
<property name="proxyInterfaces"><!--代理接口-->
<value>com.proxy.IHello</value>
</property>
<property name="target"><!--代理目标-->
<ref bean="helloSpeaker" />
</property>
<property name="
erceptorNames"><!--代理实现类-->
<list>
<value>logBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
(3)测试类SpringAOPDemo
package com.proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.File
XmlApplicationContext;
public
SpringAOPDemo {
public
void
(String
args)
{
//读取配置文件
ApplicationContext context=
File
XmlApplicationContext("advice-config.xml");
IHello helloProxy=(IHello)context.getBean("helloProxy");
helloProxy.hello("ducklyl");
}
}
运行测试类![](/icons/73505dou.gif)
Hello,ducklyl
最新评论