天天看點

bboss aop 實踐(3)構造函數依賴注入

系列文章的前兩篇介紹bboss aop架構的配置檔案文法和屬性依賴注入功能,本篇介紹bboss aop架構的構造函數依賴注入功能。

bboss-aop-1.0.5,下載下傳位址:

https://sourceforge.net/project/showfiles.php?group_id=238653&package_id=290546&release_id=658454

構造函數注入業務元件的屬性值是bboss aop架構提供的另一種依賴注入的方法,這種方法可以通過以下三種方式注入業務元件的屬性:

l         常量方式注入

l         給定屬性對應類型名稱,由架構實時建構該類型的執行個體

l         引用已有的其他業務元件

這一節詳細介紹這種依賴注入法。

準備工作,定義一個帶有構造函數的業務元件

l         元件接口

public interface ConstructorInf {

    public void testHelloworld();

}

l         元件實作

public class ConstructorImpl implements ConstructorInf {

    private String message;//驗證常量方式注入

    private AI ai;// 引用已有的其他業務元件

    private Test test;// 給定屬性對應類型名稱,由架構實時建構該類型的執行個體

    public ConstructorImpl(String message, AI ai,Test test)//依賴注入的構造函數

    {

       this.message = message;

       this.ai = ai;

       this.test = test;

    }

    public ConstructorImpl() //預設的構造函數

    public void testHelloworld() { //業務方法

       System.out.println(message);

       System.out.println(ai);

準備後元件之後,編寫bboss aop架構的業務元件配置檔案manager-constructor.xml

内容如下:

<?xml version="1.0" encoding='gb2312'?>

<manager-config>

    <manager id="constructor.a"

       singlable="true">

       <provider type="A" default="true"

           class="com.chinacreator.spi.constructor.ConstructorImpl" />   

       <construction>   

           <param value="hello world"/>

           <param refid="interceptor.a"/>

           <param type="com.chinacreator.spi.constructor.Test"/>

       </construction>

    </manager>

</manager-config>

将manager-constructor.xml檔案存儲在classes下的包com.chinacreator.spi.constructor目錄下,

再将manager-constructor.xml配置在主檔案manager-provider.xml(該檔案在classes根目錄下)中:

    ... ...   

    <managerimport file="com/chinacreator/spi/interceptor/manager-interceptor.xml" />  

    <managerimport file="com/chinacreator/spi/constructor/manager-constructor.xml" />

    ... ...

其中引用的業務元件"interceptor.a"

<param refid="interceptor.a"/>

配置在攔截器執行個體配置檔案manager-interceptor.xml中:

    <manager id="interceptor.a"

           class="com.chinacreator.spi.interceptor.A" />

       <!-- against the loop ioc rules,because constructor.a reference interceptor.a -->

       <!-- <reference fieldname="const" refid="constructor.a"/>-->

       <synchronize enabled="true">

           <method name="testInterceptorsBeforeafterWithTX"/>

           <method name="testInterceptorsBeforeAfter"/>

           <method name="testInterceptorsBeforeThrowing"/> 

           <method name="testInterceptorsBeforeThrowingWithTX">

           </method>

       </synchronize>

       <interceptor class="com.chinacreator.spi.interceptor.Insterceptor"/>

       <interceptor class="com.chinacreator.spi.interceptor.Insterceptor1"/>

       <!-- 

           在下面的節點對provider的業務方法事務進行定義

           隻要将需要進行事務控制的方法配置在transactions中即可

       -->

       <transactions>

           <method name="testInterceptorsBeforeThrowing">