天天看點

用代碼學習Spring:IoC、AOP(上)

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;

    }

}