//代理类
package com.gc.action;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LogAround implements MethodInterceptor {
private Logger logger = Logger.getLogger(this.getClass().getName());
//负责输出日志信息的代码
public Object invoke(MethodInvocation mi)throws Throwable{
logger.log(Level.INFO,mi.getArguments()[0]+" 开始审核数据...");
try{
Object result = mi.proceed();
//返回值即是被调用的方法的返回值
return result;
} finally {
logger.log(Level.INFO,mi.getArguments()[0]+" 审核数据结束...");
}
}
}
//接口1
package com.gc.impl;
public interface TimeBookInterface {
//负责具体的业务逻辑
public void doAuditing(String name);
}
//实现接口1的类
package com.gc.action;
import com.gc.impl.TimeBookInterface;
public class TimeBook implements TimeBookInterface {
public void doAuditing(String name) {
System.out.println(name + " 正在审核...");
}
}
//接口2
package com.gc.impl;
public interface FinanceInterface {
public void doCheck(String name);
}
//实现接口2的类
package com.gc.action;
import com.gc.impl.FinanceInterface;
public class Finance implements FinanceInterface {
public void doCheck(String name) {
System.out.println(name + " 财务观战...");
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: beans1.xml,v 1.3 2006/08/20 19:08:40 jhoeller Exp $ -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="log" class="com.gc.action.LogAround" />
<bean id="timeBook" class="com.gc.action.TimeBook" />
<bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.gc.impl.TimeBookInterface</value>
</property>
<property name="target">
<ref bean="timeBook"/>
</property>
<!-- 指定代理类 -->
<property name="interceptorNames">
<list>
<value>log</value>
</list>
</property>
</bean>
<bean id="finance" class="com.gc.action.Finance" />
<bean id="logProxy2" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.gc.impl.FinanceInterface</value>
</property>
<property name="target">
<ref bean="finance"/>
</property>
<!-- 指定代理类 -->
<property name="interceptorNames">
<list>
<value>log</value>
</list>
</property>
</bean>
</beans>
//测试main函数
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.impl.FinanceInterface;
import com.gc.impl.TimeBookInterface;
public class TestHelloWorld {
public static void main(String[] args){
//通过ApplicationContext获取XML
ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
TimeBookInterface timeBookProxy = (TimeBookInterface)actx.getBean("logProxy");
timeBookProxy.doAuditing("张三");
FinanceInterface finance = (FinanceInterface)actx.getBean("logProxy2");
finance.doCheck("李四");
}
}
运行结果:
[INFO ] 2009-11-10 22:04:55 com.gc.action.LogAround - 张三 开始审核数据...
张三 正在审核...
[INFO ] 2009-11-10 22:04:55 com.gc.action.LogAround - 张三 审核数据结束...
[INFO ] 2009-11-10 22:04:55 com.gc.action.LogAround - 李四 开始审核数据...
李四 财务观战...
[INFO ] 2009-11-10 22:04:55 com.gc.action.LogAround - 李四 审核数据结束...