国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

用代碼學習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;
    }
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香河县| 河曲县| 吉安县| 兴和县| 丰台区| 崇礼县| 张家川| 余江县| 正安县| 永定县| 连城县| 琼海市| 衡山县| 石楼县| 深水埗区| 青海省| 新干县| 封丘县| 利津县| 石楼县| 册亨县| 灵丘县| 河津市| 贵德县| 凯里市| 湖南省| 金川县| 叶城县| 兰州市| 阿拉善左旗| 新泰市| 肇庆市| 凌源市| 托克托县| 徐闻县| 渝中区| 娄烦县| 仪征市| 界首市| 寿阳县| 霞浦县|