首先是接口定义:
package com.gc.impl;


public interface FinanceInterface ...{

public void doCheck(String name);
public void doSaying();
}
package com.gc.action;

import com.gc.impl.*;


public class Finance implements FinanceInterface...{

public void doCheck(String name)

...{
System.out.println("doChecking...");
}
public void doSaying()

...{
System.out.println("doSaying...");
}
}
package com.gc.action;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


public class LogAround implements MethodInterceptor...{

public Object invoke(MethodInvocation mi)throws Throwable

...{
System.out.println("Starting...");
try

...{
Object result=mi.proceed();
return result;
}
finally

...{
System.out.println("Ending...");
}
}
}
<?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="log" class="com.gc.action.LogAround" />
<bean id="Finance" class="com.gc.action.Finance" />
<!-- 这里是先定义一个适配器(advisor)然后用来赋给下面bean中interceptorNames属性的value
package com.gc.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.impl.FinanceInterface;


public class TestFinance ...{


/** *//**
* @param args
*/

public static void main(String[] args) ...{
// TODO Auto-generated method stub

ApplicationContext acxt=new FileSystemXmlApplicationContext("applicationContext.xml");
FinanceInterface fi=(FinanceInterface)acxt.getBean("logProxy1");
fi.doCheck(""); //因为在xml文件中配置的方法中本方法不对应所以它不适合文件中配置的通知,所以没有输出通知的内容!
System.out.println();
fi.doSaying();
}

}
package com.gc.impl;

public interface FinanceInterface ...{
public void doCheck(String name);
public void doSaying();
}
然后是接口实现类:
package com.gc.action;
import com.gc.impl.*;

public class Finance implements FinanceInterface...{
public void doCheck(String name)
...{
System.out.println("doChecking...");
}
public void doSaying()
...{
System.out.println("doSaying...");
}
}
接着是插入到切入点的代码(即通知 advice)本例中用的是前后都有通知的方式:(当然可以用不同的通知方式)
package com.gc.action;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class LogAround implements MethodInterceptor...{
public Object invoke(MethodInvocation mi)throws Throwable
...{
System.out.println("Starting...");
try
...{
Object result=mi.proceed();
return result;
}
finally
...{
System.out.println("Ending...");
}
}
}
接着是最重要的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="log" class="com.gc.action.LogAround" />
<bean id="Finance" class="com.gc.action.Finance" />
<!-- 这里是先定义一个适配器(advisor)然后用来赋给下面bean中interceptorNames属性的value 从这里也可以看出适配器其实就是一个定义advice和切入点之间关系的一个bean而已,没什么神秘的东西...
-->
<bean id="logAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="log" />
</property>
<property name="patterns">
<value>.*doSaying.*</value>
</property>
</bean>
<bean id="logProxy1" 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>logAdvisor</value>
</list>
</property>
</bean>
</beans>
<bean id="logAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref bean="log" />
</property>
<property name="patterns">
<value>.*doSaying.*</value>
</property>
</bean>
<bean id="logProxy1" 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>logAdvisor</value>
</list>
</property>
</bean>
</beans>
最后是测试类:
package com.gc.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.impl.FinanceInterface;

public class TestFinance ...{

/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
ApplicationContext acxt=new FileSystemXmlApplicationContext("applicationContext.xml");
FinanceInterface fi=(FinanceInterface)acxt.getBean("logProxy1");
fi.doCheck(""); //因为在xml文件中配置的方法中本方法不对应所以它不适合文件中配置的通知,所以没有输出通知的内容!
System.out.println();
fi.doSaying();
}
}
本文介绍如何使用 Spring AOP 进行日志记录的实现过程,包括定义接口、实现类、AOP 通知类及配置 XML 文件等关键步骤。
1328

被折叠的 条评论
为什么被折叠?



