国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
    }
}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂平县| 阿克苏市| 易门县| 灵川县| 吉安市| 莱西市| 武鸣县| 柳林县| 宣城市| 渭源县| 中卫市| 察哈| 合水县| 宜昌市| 定安县| 嘉定区| 平顺县| 揭东县| 禄丰县| 涞水县| 阿坝县| 金平| 新龙县| 永兴县| 和龙市| 厦门市| 拉孜县| 云安县| 乌什县| 林甸县| 长顺县| 柳河县| 土默特左旗| 百色市| 吉首市| 洛川县| 阳城县| 集安市| 宁安市| 深水埗区| 云龙县|