<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    狂奔 lion

    自強不息

    用代碼一步步學習Spring:IoC,AOP

    1 從http://www.springframework.org下載Spring
    2 用eclipse新建Java項目
    3 建立我們的業(yè)務方法接口
    public interface BusinessObject {
        
    public void doSomething();
        
    public void doAnotherThing();
    }


    4 實現業(yè)務方法,注意這是的setWords使用了依賴注入,所謂依賴注入就是把配置文件中的字符串什么的在程序運行時“自動”放到我們的程序中來。如果不是這樣,我們就只能在代碼中固化這些東西,從而違背了面向對象的依賴倒置原則,還有一種滿足依賴倒置的方法,即依賴查詢,這就是所謂的factory模式,即在代碼中請求某種抽象的東西,然后根據配置得到它,但這種辦法向對于依賴注入多了對環(huán)境的依賴,且代碼冗余,EJB的JNDI查詢就屬于這種。另外我們的Spring配置文件是以bean為核心的,就是我們寫的一個類,在XML中描述它的名稱、位置和涵蓋的內容、關系。
    public class BusinessObjectImpl implements BusinessObject {
        
    private String words;
        
    public void setWords(String words){
            
    this.words = words;
        }
        
    public void doSomething() {
            Log log 
    = LogFactory.getLog(this.getClass());
            log.info(words);
        }
        
    public void doAnotherThing() {
            Log log 
    = LogFactory.getLog(this.getClass());
            log.info(
    "Another thing");
        }

    }

    5 建立一個運行方法類,從配置文件spring-beans.xml中讀入bo這個類的定義,然后實例化一個對象
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;


    public class Main {
        
    public static void main(String[] args){
            XmlBeanFactory xbf 
    = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
            BusinessObject bo 
    = (BusinessObject)xbf.getBean("bo");
            bo.doSomething();
            bo.doAnotherThing();
        }
    }

    6 建立一個攔截器類invoke是MethodInterceptor必須實現的方法,表示攔截時的動作,大家仔細體會代碼中的含義
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;


    public class MyInterceptor implements MethodInterceptor {
        
    private String before, after;
        
    public void setAfter(String after) {
            
    this.after = after;
        }
        
    public void setBefore(String before) {
            
    this.before = before;
        }
        
    public Object invoke(MethodInvocation invocation) throws Throwable {
            Log log 
    = LogFactory.getLog(this.getClass());
            log.info(before);
            Object rval 
    = invocation.proceed();
            log.info(after);
            
    return rval;
        }
    }

    7 建立配置文件組織上面的類之間的關系,AOP有切入點和增強這兩個重要的概念,把兩個概念結合到一起,就是一個在某個方法執(zhí)行的時候附加執(zhí)行,切入點表示在哪里附加,增強表示附加什么,配置文件中的myPointcut表示切入點,myInterceptor表示增強的內容,myAdvisor表示增強器,即兩者的結合,在bo這個bean中,我們把這個增強器附加到了bo這個bean上。

    <?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="businessObjectImpl" class="BusinessObjectImpl">
            
    <property name="words">
                
    <value>正在執(zhí)行業(yè)務方法</value>
            
    </property>
        
    </bean>
        
    <bean id="myInterceptor" class="MyInterceptor">
            
    <property name="before">
                
    <value>執(zhí)行業(yè)務方法前</value>
            
    </property>
            
    <property name="after">
                
    <value>執(zhí)行業(yè)務方法后</value>
            
    </property>
        
    </bean>
        
    <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        
    <property name="patterns">
            
    <list>
                
    <value>BusinessObject.doSomething</value>
            
    </list>
        
    </property>
        
    </bean>
        
    <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
          
    <property name="pointcut" ref="myPointcut"/>
          
    <property name="advice" ref="myInterceptor"/>
        
    </bean>
        
    <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
            
    <property name="target">
                
    <ref local="businessObjectImpl"/>
            
    </property>
            
    <property name="proxyInterfaces">
                
    <value>BusinessObject</value>
            
    </property>
            
    <property name="interceptorNames">
                
    <list>
                    
    <value>myInterceptor</value>
                    
    <value>myAdvisor</value>
                
    </list>
            
    </property>
        
    </bean>
    </beans>


    8 運行Main類,觀察控制臺輸出結果,重新審查代碼,反思為什么會出現這種結果。

     @2008 楊一. 版權所有. 保留所有權利

    posted on 2006-12-11 22:55 楊一 閱讀(2558) 評論(1)  編輯  收藏 所屬分類: Java EE

    評論

    # re: 用代碼一步步學習Spring:IoC,AOP 2007-04-11 20:57 spring study

    沒有創(chuàng)建myAdvisor這個類阿 運行不了   回復  更多評論   

    <2006年12月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    導航

    公告

    本人在blogjava上發(fā)表的文章及隨筆除特別聲明外均為原創(chuàng)或翻譯,作品受知識產權法保護并被授權遵從 知識分享協(xié)議:署名-非商業(yè)性使用-相同方式共享 歡迎轉載,請在轉載時注明作者姓名(楊一)及出處(www.tkk7.com/yangyi)
    /////////////////////////////////////////
    我的訪問者

    常用鏈接

    留言簿(5)

    隨筆分類(55)

    隨筆檔案(55)

    相冊

    Java

    其他技術

    生活

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    自強不息


    用心 - 珍惜時間,勇于創(chuàng)造
    主站蜘蛛池模板: 国产亚洲精品第一综合| 久久久久亚洲AV综合波多野结衣| 亚洲精品色午夜无码专区日韩| 男人的天堂av亚洲一区2区| 成人免费无码大片a毛片软件 | 色天使亚洲综合一区二区| 成年女人午夜毛片免费看| 亚洲人av高清无码| 在线观看国产情趣免费视频| 亚洲AV无码专区在线观看成人 | 今天免费中文字幕视频| 亚洲欧洲第一a在线观看| 玖玖在线免费视频| 亚洲综合色一区二区三区小说| a拍拍男女免费看全片| 91在线亚洲综合在线| 国产又大又粗又硬又长免费| 国产精品自拍亚洲| 国产av无码专区亚洲av果冻传媒| 久久青青草原国产精品免费| 91亚洲自偷在线观看国产馆| 日韩在线免费播放| 一道本在线免费视频| 国产亚洲无线码一区二区| 麻豆视频免费观看| 粉色视频在线观看www免费| 国产精品亚洲成在人线| 在线观看免费人成视频色| 无码一区二区三区亚洲人妻| 2048亚洲精品国产| 免费观看激色视频网站(性色)| 亚洲av无码成人精品区一本二本| 亚洲色欲色欲www在线丝 | 亚洲熟妇AV一区二区三区宅男| 免费99热在线观看| 免费A级毛片无码专区| 亚洲熟妇AV日韩熟妇在线| 亚洲色无码一区二区三区| 国语成本人片免费av无码| 精品国产福利尤物免费| 亚洲熟女精品中文字幕|