隨筆-109  評論-187  文章-25  trackbacks-0

          最近深入的學習了一下 Spring, 感受到了 AOP 的威力,以前看過 BEA DEV2DEV 雜志,有一期專門的 AOP ,里面很詳細講的,看了有一年多了,今天真正來作個例子

          ?

          這個例子也是從 dev2dev 上看的,

          1 :建一個接口 IBusinessLogic

          ?

          package test;

          ?

          public interface IBusinessLogic {

          ?????? public void foo(String i);

          ?

          }

          2 :建一個接口實現的類

          package test;

          ?

          public class BusinessLogic implements IBusinessLogic {

          ?

          ?????? public void foo(String i) {

          ????????????? System.out.println("Inside BusinessLogic.foo()");

          ?

          ?????? }

          ?

          }

          ?

          3 建立一個應用程序 MainApplication

          ?

          ?????? public static void main(String[] args) {

          ????????????? ?// Read the configuration file

          ??????? ApplicationContext ctx =

          ????????? new FileSystemXmlApplicationContext(

          ??????????? "E:\\work\\Test\\src\\spring-config.xml");

          ??????? //Instantiate an object

          ??????? IBusinessLogic testObject =

          ????????? (IBusinessLogic) ctx.getBean("businesslogicbean");

          ??????? testObject.foo("11");

          ?????? }

          ?

          <? xml version = "1.0" encoding = "UTF-8" ?>

          <! DOCTYPE beans PUBLIC

          ????????? "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

          ?

          < beans >

          ?

          ?? <!-- Bean configuration -->

          ?? < bean id = "businesslogicbean"

          ?? class = "org.springframework.aop.framework.ProxyFactoryBean" >

          ????? < property name = "proxyInterfaces" >

          ???????? < value > test.IBusinessLogic </ value >

          ????? </ property >

          ????? < property name = "target" >

          ???????? < ref local = "beanTarget" />

          ????? </ property >

          ???? <property name="interceptorNames">

          ???????? <list>

          ??????????? <value>theTracingBeforeAdvisor</value>

          ??????????? <value>theTracingAfterAdvisor</value>

          ???????? </list>

          ???????? </property>

          ????

          ?? </ bean >

          ?? <!-- Bean Classes -->

          ?? < bean id = "beanTarget"

          ?? class = "test.BusinessLogic" />

          ??

          ????? <bean id="theTracingBeforeAdvisor"

          ????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

          ????? <property name="advice">

          ???????? <ref local="theTracingBeforeAdvice"/>

          ????? </property>

          ????? <property name="pattern">

          ???????? <value>.*</value>

          ????? </property>

          ?? </bean>

          ???

          ?? <!-- Advisor pointcut definition for after advice -->

          ?? <bean id="theTracingAfterAdvisor"

          ????? class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

          ????? <property name="advice">

          ???????? <ref local="theTracingAfterAdvice"/>

          ????? </property>

          ????? <property name="pattern">

          ???????? <value>.*</value>

          ????? </property>

          ?? </bean>

          ?

          ?? <!-- Advice classes -->

          ?? <bean id="theTracingBeforeAdvice"

          ????? class="test.TracingBeforeAdvice"/>

          ?? <bean id="theTracingAfterAdvice"

          ????? class="test.TracingAfterAdvice"/>

          ?????

          ?????

          ?

          </ beans >

          ?

          紅色的暫時不配置,你就可以看到打印出來的結果

          Inside BusinessLogic.foo()

          ?

          4 :建立 2 個類 TracingBeforeAdvice

          ?

          package test;

          ?

          import java.lang.reflect.Method;

          ?

          import org.springframework.aop.MethodBeforeAdvice;

          ?

          public class TracingBeforeAdvice implements MethodBeforeAdvice {

          ?????? public void before(Method m, Object[] args, Object target) throws Throwable {

          ????????????? System.out.println(m.getClass());

          ????????????? System.out.println(args);

          ????????????? System.out.println(target);

          ????????????? System.out.println("Hello world! (by " + this.getClass().getName()

          ??????????????????????????? + ")");

          ?????? }

          }

          ?

          ?

          TracingAfterAdvice

          package test;

          ?

          import java.lang.reflect.Method;

          ?

          import org.springframework.aop.AfterReturningAdvice;

          ?

          public class TracingAfterAdvice

          implements AfterReturningAdvice

          {

          ?public void afterReturning(Object object,

          ????????????????????????? Method m,

          ????????????????????????? Object[] args,

          ????????????????????????? Object target)

          ????????????????????????? throws Throwable

          ?{

          ???? System.out.println(

          ?????? "Hello world! (by " +

          ?????? this.getClass().getName() +

          ?????? ")");

          ?}

          }

          然后把紅色部分的配置上去,

          運行 MainApplication

          你就可以看到

          //class java.lang.reflect.Method

          //[Ljava.lang.Object;@3a9bba

          //test.BusinessLogic@1c5ddc9

          Hello world! (by test.TracingBeforeAdvice)

          Inside BusinessLogic.foo()

          Hello world! (by test.TracingAfterAdvice)

          ?

          AOP 在方法開始和技術的時候起到了作用!!!

          posted on 2006-05-18 18:04 小小程序程序員混口飯吃 閱讀(400) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 沿河| 沂水县| 古浪县| 双峰县| 蓝田县| 庆元县| 金寨县| 长岛县| 肥乡县| 砚山县| 吉隆县| 贺州市| 舒城县| 天津市| 湘乡市| 松原市| 宜章县| 澳门| 综艺| 汉沽区| 荔浦县| 万荣县| 沅陵县| 平陆县| 海盐县| 贞丰县| 兰州市| 河曲县| 磴口县| 惠安县| 彭山县| 股票| 玛纳斯县| 朔州市| 客服| 达日县| 东源县| 孝昌县| 江陵县| 九台市| 镇坪县|