一直就用spring的IOC,遺憾spring的另一重要組成部分AOP卻沒用過,所以近幾天抽空研究了下AOP,學了些東西,在這里記錄下spring2.0的aop配置,以一個簡單的記錄日志的實例來說明,先介紹下用XMLSchema來配置,下一篇介紹annotation配置,廢話不多說,開始吧
先新建個web工程,將spring的包加進去,為方便就把全部的jar包加進去。
先來看個接口,很簡單就兩個方法
public interface Print {
public String print(String name);
public String sleep(String name);
}
接下來是實現(xiàn)類
public class SystemPrint implements Print{
public String print(String name){
String result="hello " + name;
System.out.println(result);
return result;
}
public String sleep(String name){
String result=name+" is sleep now";
System.out.println(result);
return result;
}
}
下面是所要織入的代碼,也就是我們要用來記錄日志的
public class GetLog {
public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
String reslut = (String)joinpoint.proceed();
//這里是記錄日志的
System.out.println("result==="+reslut);
}
}
再來看spring配置文件,沒有注釋的很清楚,可以去網上查查
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!--這個bean是作為切面 -->
<bean id="log" class="spring2aop.GetLog"></bean>
<!--
注意這里:expression="execution(* spring2aop.*.print*(..))"
括號里面第一個*號代表返回值 接下來 spring2aop.*. 是你要切入的代碼的大概路徑,這里為什么用大概路徑來形容呢
因為這里的意思是符合以spring2aop的路徑都會作為選擇的對象,也不詳細介紹,查下就明白了, print*(..)是指
方法名以print開頭的都符合,括號里面的 .. 表示參數(shù)是隨意的都可以。
-->
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="printMethods" expression="execution(* spring2aop.*.print*(..))"/>
<aop:after-returning method="getLog" pointcut-ref="printMethods" returning="retVal"/>
</aop:aspect>
</aop:config>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="sleepMethods" expression="execution(* spring2aop.*.sle*(..))"/>
<aop:after-returning method="getLog" pointcut-ref="sleepMethods" returning="retVal"/>
</aop:aspect>
</aop:config>
<!--要織入代碼的bean-->
<bean id="print" class="spring2aop.SystemPrint"></bean>
</beans>
測試類:
public class Test {
/**
* @Description 方法實現(xiàn)功能描述
* @param args
* void
* @throws 拋出異常說明
*/
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext20.xml");
Print t =(Print)act.getBean("print");
t.print("ding");
System.out.println("-----------------");
t.sleep("laoding");
}
}
運行這個類,得到如下結果:
hello ding
hello ding
result===hello ding
-----------------
laoding is sleep now
laoding is sleep now
result===laoding is sleep now
這里的hello ding 打印了兩次,不用擔心,這是因為執(zhí)行到getLog切面類的
String reslut = (String)joinpoint.proceed();這句代碼的時候再執(zhí)行了一次,這句代碼是取回
返回結果的,可以設置個斷點來測試下好了這里就輸出的result就是記錄的日志,當然
這里只是個很簡單的實現(xiàn),但是很簡單的實現(xiàn)卻很容易說清楚原理。
posted on 2008-11-25 18:14
老丁 閱讀(3456)
評論(4) 編輯 收藏 所屬分類:
spring