麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

用代碼學習Spring:IoC、AOP

2019-11-18 15:28:29
字體:
來源:轉載
供稿:網友

1 從http://www.sPRingframework.org下載Spring
2 用eclipse新建java項目
3 建立我們的業務方法接口
public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

4 實現業務方法,注重這是的setWords使用了依靠注入,所謂依靠注入就是把配置文件中的字符串什么的在程序運行時“自動”放到我們的程序中來。假如不是這樣,我們就只能在代碼中固化這些東西,從而違反了面向對象的依靠倒置原則,還有一種滿足依靠倒置的方法,即依靠查詢,這就是所謂的factory模式,即在代碼中請求某種抽象的東西,然后根據配置得到它,但這種辦法向對于依靠注入多了對環境的依靠,且代碼冗余,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");
    }

}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();
    }
}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;
    }
}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;
    }
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国内精品久久久久久影视8 嫩草影院在线观看网站成人 | 亚洲生活片 | 国产精品性夜天天视频 | 成人一区二区在线观看视频 | 91精品观看91久久久久久国产 | 久久老司机| 免费色片 | 日日鲁夜夜视频热线播放 | 视频一区二区不卡 | 二级大黄大片高清在线视频 | 亚洲第一成人久久网站 | 视频一区二区久久 | 一区二区三区日韩在线 | 免费a级网站 | 亚洲精久久| 欧美性受ⅹ╳╳╳黑人a性爽 | 国产无区一区二区三麻豆 | 美女视频网站黄色 | 一级毛片免费高清视频 | 亚洲国产在 | 国产国语毛片 | 国产va在线观看免费 | 亚洲影视中文字幕 | 中文字幕在线观看91 | 欧美一级性 | 成人羞羞视频在线观看 | 羞羞网站入口 | 99爱精品在线 | 国产精品久久久久网站 | 精品国产一区二区三区四区在线 | 久久经典国产视频 | 久久国产精品99国产 | www.99tv| 亚洲精品在线观看免费 | 天天操天天碰 | 国产高清美女一级毛片久久 | 国产成人高清成人av片在线看 | 91九色视频在线播放 | av视在线 | av在线免费观看中文字幕 | 免费看性xxx高清视频自由 |