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;
}
}
新聞熱點
疑難解答