熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

用代碼學習Spring:IoC、AOP

2013-11-23 20:38:28  來源: Java開源技術 

   從下載Spring
 用eclipse新建Java項目
 建立我們的業務方法接口
public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import monsloggingLog;
import monsloggingLogFactory;public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import monsloggingLog;
import monsloggingLogFactory;


 實現業務方法注意這是的setWords使用了依賴注入所謂依賴注入就是把配置文件中的字符串什麼的在程序運行時自動放到我們的程序中來如果不是這樣我們就只能在代碼中固化這些東西從而違背了面向對象的依賴倒置原則還有一種滿足依賴倒置的方法即依賴查詢這就是所謂的factory模式即在代碼中請求某種抽象的東西然後根據配置得到它但這種辦法向對於依賴注入多了對環境的依賴且代碼冗余EJB的JNDI查詢就屬於這種另外我們的Spring配置文件是以bean為核心的就是我們寫的一個類在XML中描述它的名稱位置和涵蓋的內容關系
public class BusinessObjectImpl implements BusinessObject {
    private String words;
    public void setWords(String words){
        thiswords = words;
    }
    public void doSomething() {
        Log log = LogFactorygetLog(thisgetClass());
        (words);
    }
    public void doAnotherThing() {
        Log log = LogFactorygetLog(thisgetClass());
        (Another thing);
    }

}public class BusinessObjectImpl implements BusinessObject {
    private String words;
    public void setWords(String words){
        thiswords = words;
    }
    public void doSomething() {
        Log log = LogFactorygetLog(thisgetClass());
        (words);
    }
    public void doAnotherThing() {
        Log log = LogFactorygetLog(thisgetClass());
        (Another thing);
    }

}

 建立一個運行方法類從配置文件springbeansxml中讀入bo這個類的定義然後實例化一個對象
import orgspringframeworkbeansfactoryxmlXmlBeanFactory;
import orgreioClassPathResource;


public class Main {
    public static void main(String[] args){
        XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource(springbeansxml));
        BusinessObject bo = (BusinessObject)xbfgetBean(bo);
        bodoSomething();
        bodoAnotherThing();
    }
}import orgspringframeworkbeansfactoryxmlXmlBeanFactory;
import orgreioClassPathResource;


public class Main {
    public static void main(String[] args){
        XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource(springbeansxml));
        BusinessObject bo = (BusinessObject)xbfgetBean(bo);
        bodoSomething();
        bodoAnotherThing();
    }
}

 建立一個攔截器類invoke是MethodInterceptor必須實現的方法表示攔截時的動作大家仔細體會代碼中的含義
import orgaopallianceinterceptMethodInterceptor;
import orgaopallianceinterceptMethodInvocation;
import monsloggingLog;
import monsloggingLogFactory;


public class MyInterceptor implements MethodInterceptor {
    private String before after;
    public void setAfter(String after) {
        thisafter = after;
    }
    public void setBefore(String before) {
        thisbefore = before;
    }
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Log log = LogFactorygetLog(thisgetClass());
        (before);
        Object rval = invocationproceed();
        (after);
        return rval;
    }
}import orgaopallianceinterceptMethodInterceptor;
import orgaopallianceinterceptMethodInvocation;
import monsloggingLog;
import monsloggingLogFactory;


public class MyInterceptor implements MethodInterceptor {
    private String before after;
    public void setAfter(String after) {
        thisafter = after;
    }
    public void setBefore(String before) {
        thisbefore = before;
    }
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Log log = LogFactorygetLog(thisgetClass());
        (before);
        Object rval = invocationproceed();
        (after);
        return rval;
    }
}

   建立配置文件組織上面的類之間的關系AOP有切入點和增強這兩個重要的概念把兩個概念結合到一起就是一個在某個方法執行的時候附加執行切入點表示在哪裡附加增強表示附加什麼配置文件中的myPointcut表示切入點myInterceptor表示增強的內容myAdvisor表示增強器即兩者的結合在bo這個bean中我們把這個增強器附加到了bo這個bean上

<?xml version= encoding=UTF?>
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
    <bean id=businessObjectImpl class=BusinessObjectImpl>
        <property name=words>
            <value>正在執行業務方法</value>
        </property>
    </bean>
    <bean id=myInterceptor class=MyInterceptor>
        <property name=before>
            <value>執行業務方法前</value>
        </property>
        <property name=after>
            <value>執行業務方法後</value>
        </property>
    </bean>
    <bean id=myPointcut class=orgspringframeworkaopsupportJdkRegexpMethodPointcut>
    <property name=patterns>
        <list>
            <value>BusinessObjectdoSomething</value>
        </list>
    </property>
    </bean>
    <bean id=myAdvisor class=orgspringframeworkaopsupportDefaultPointcutAdvisor>
      <property name=pointcut ref=myPointcut/>
      <property name=advice ref=myInterceptor/>
    </bean>
    <bean id=bo class=orgspringframeworkaopframeworkProxyFactoryBean>
        <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><?xml version= encoding=UTF?>
<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN beansdtd>
<beans>
    <bean id=businessObjectImpl class=BusinessObjectImpl>
        <property name=words>
            <value>正在執行業務方法</value>
        </property>
    </bean>
    <bean id=myInterceptor class=MyInterceptor>
        <property name=before>
            <value>執行業務方法前</value>
        </property>
        <property name=after>
            <value>執行業務方法後</value>
        </property>
    </bean>
    <bean id=myPointcut class=orgspringframeworkaopsupportJdkRegexpMethodPointcut>
    <property name=patterns>
        <list>
            <value>BusinessObjectdoSomething</value>
        </list>
    </property>
    </bean>
    <bean id=myAdvisor class=orgspringframeworkaopsupportDefaultPointcutAdvisor>
      <property name=pointcut ref=myPointcut/>
      <property name=advice ref=myInterceptor/>
    </bean>
    <bean id=bo class=orgspringframeworkaopframeworkProxyFactoryBean>
        <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>


 運行Main類觀察控制台輸出結果重新審查代碼反思為什麼會出現這種結果


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28815.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.