用代碼一步步學(xué)習(xí)Spring:IoC,AOP
1 從http://www.springframework.org下載Spring2 用eclipse新建Java項(xiàng)目
3 建立我們的業(yè)務(wù)方法接口
public interface BusinessObject {
public void doSomething();
public void doAnotherThing();
}
public void doSomething();
public void doAnotherThing();
}
4 實(shí)現(xiàn)業(yè)務(wù)方法,注意這是的setWords使用了依賴注入,所謂依賴注入就是把配置文件中的字符串什么的在程序運(yùn)行時(shí)“自動(dòng)”放到我們的程序中來。如果不是這樣,我們就只能在代碼中固化這些東西,從而違背了面向?qū)ο蟮囊蕾嚨怪迷瓌t,還有一種滿足依賴倒置的方法,即依賴查詢,這就是所謂的factory模式,即在代碼中請(qǐng)求某種抽象的東西,然后根據(jù)配置得到它,但這種辦法向?qū)τ谝蕾囎⑷攵嗔藢?duì)環(huán)境的依賴,且代碼冗余,EJB的JNDI查詢就屬于這種。另外我們的Spring配置文件是以bean為核心的,就是我們寫的一個(gè)類,在XML中描述它的名稱、位置和涵蓋的內(nèi)容、關(guān)系。
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");
}
}
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 建立一個(gè)運(yùn)行方法類,從配置文件spring-beans.xml中讀入bo這個(gè)類的定義,然后實(shí)例化一個(gè)對(duì)象
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.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 建立一個(gè)攔截器類invoke是MethodInterceptor必須實(shí)現(xiàn)的方法,表示攔截時(shí)的動(dòng)作,大家仔細(xì)體會(huì)代碼中的含義
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.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;
}
}
7 建立配置文件組織上面的類之間的關(guān)系,AOP有切入點(diǎn)和增強(qiáng)這兩個(gè)重要的概念,把兩個(gè)概念結(jié)合到一起,就是一個(gè)在某個(gè)方法執(zhí)行的時(shí)候附加執(zhí)行,切入點(diǎn)表示在哪里附加,增強(qiáng)表示附加什么,配置文件中的myPointcut表示切入點(diǎn),myInterceptor表示增強(qiáng)的內(nèi)容,myAdvisor表示增強(qiáng)器,即兩者的結(jié)合,在bo這個(gè)bean中,我們把這個(gè)增強(qiáng)器附加到了bo這個(gè)bean上。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessObjectImpl" class="BusinessObjectImpl">
<property name="words">
<value>正在執(zhí)行業(yè)務(wù)方法</value>
</property>
</bean>
<bean id="myInterceptor" class="MyInterceptor">
<property name="before">
<value>執(zhí)行業(yè)務(wù)方法前</value>
</property>
<property name="after">
<value>執(zhí)行業(yè)務(wù)方法后</value>
</property>
</bean>
<bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>BusinessObject.doSomething</value>
</list>
</property>
</bean>
<bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="myPointcut"/>
<property name="advice" ref="myInterceptor"/>
</bean>
<bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="businessObjectImpl"/>
</property>
<property name="proxyInterfaces">
<value>BusinessObject</value>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
<value>myAdvisor</value>
</list>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessObjectImpl" class="BusinessObjectImpl">
<property name="words">
<value>正在執(zhí)行業(yè)務(wù)方法</value>
</property>
</bean>
<bean id="myInterceptor" class="MyInterceptor">
<property name="before">
<value>執(zhí)行業(yè)務(wù)方法前</value>
</property>
<property name="after">
<value>執(zhí)行業(yè)務(wù)方法后</value>
</property>
</bean>
<bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>BusinessObject.doSomething</value>
</list>
</property>
</bean>
<bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="myPointcut"/>
<property name="advice" ref="myInterceptor"/>
</bean>
<bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="businessObjectImpl"/>
</property>
<property name="proxyInterfaces">
<value>BusinessObject</value>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
<value>myAdvisor</value>
</list>
</property>
</bean>
</beans>
8 運(yùn)行Main類,觀察控制臺(tái)輸出結(jié)果,重新審查代碼,反思為什么會(huì)出現(xiàn)這種結(jié)果。
@2008 楊一. 版權(quán)所有. 保留所有權(quán)利
posted on 2006-12-11 22:55 楊一 閱讀(2561) 評(píng)論(1) 編輯 收藏 所屬分類: Java EE