[英]Java configuration and dependency injection (akin to Springs IoC vs. Weld / Guice)
假設我有一個我想配置的ListCreator
類。 我希望能夠告訴它排序順序以及如何輸出我的表。 因此,我有TableWriter
sortDescending
屬性和TableWriter
接口,它由PdfTableWriter
(也由XlsTableWriter
)實現。 在這個例子中,我認為配置和DI是相輔相成的。 我想寫一些類似Spring(偽)的例子:
<beans>
<bean id="ListCreator" class="ModularListCreator">
<property name="tableWriter">
<ref local="TableWriter"/>
</property>
<property name="sortDescending">
<value>true</value>
</property>
</bean>
<bean id="TableWriter" class="PdfTableWriter"> </bean>
</beans>
現在Spring可以做到這一點,但似乎Weld&Guice不能。 例如,Weld允許您在beans.xml中選擇備選方案,但僅適用於整個應用程序。 如果我想同時擁有一個ListCreator
用於PDF而另一個用於XLS,該怎么辦?
我目前還沒有得到Weld和Guice的范圍,因為它們似乎不允許太多的配置。 這似乎只是減輕了編寫new
或實現自己的工廠的需要。 例如,EJB注入做的相同,這很好,但是整個配置部分在哪里(選擇哪個實例具有我實際想要的參數)。
來到真正的問題:我不想使用Spring,因為它似乎是開銷。 我更喜歡使用JSR指定的干凈小巧的東西。 有什么建議么?
如果您需要靈活的基於描述的解決方案, InPUT提供了一種方法。 我創建了整個示例,並將其添加到示例部分 。
代碼很小,包括:
Design config = new Design("config.xml");
ListCreator creator = config.getValue("ListCreator");
假設您有一個config.xml InPUT 設計 ,其中包含InPUT語法中的設置:
<SValue id="ListCreator">
<NValue id="SortDescending" value="false"/>
<SValue id="TableWriter" value="Xls"/>
</SValue>
為此,您必須按如下方式定義設計空間 :
<SParam id="ListCreator">
<NParam id="SortDescending" type="boolean" />
<SParam id="TableWriter">
<SChoice id="Xls"/>
<SChoice id="Pdf"/>
</SParam>
</SParam>
您現在可以在代碼映射中為Java實現定制獨立於編程語言的設計空間:
<Mapping id="ListCreator" type="test.ListCreator" constructor="TableWriter SortDescending"/>
<Mapping id="ListCreator.TableWriter" type="test.TableWriter"/>
<Mapping id="ListCreator.TableWriter.Xls" type="test.XlsTableWriter"/>
<Mapping id="ListCreator.TableWriter.Pdf" type="test.PdfTableWriter"/>
從這里,免費擴展和自定義,無需觸摸代碼。 您提到了多個ListCreator實例的情況。 您必須進行3次更改:
1)設計空間:
<SValue id="ListCreator" type="[]">
2)設計(例如):
<SValue id="ListCreator">
<SValue id="1">
<NValue id="SortDescending" value="true"/>
<SValue id="TableWriter" value="Pdf"/>
</SValue>
<SValue id="2">
<NValue id="SortDescending" value="false"/>
<SValue id="TableWriter" value="Xls"/>
</SValue>
</SValue>
3)准備好接收一個數組(代碼):
ListCreator[] creators = config.getValue("ListCreator");
您可以決定描述符中的數字和替代項; 條目以定義的順序到達。 這適用於多個維度(例如“[] [] []”)。 您可以在將來添加具有更多參數的替代表編寫器,或者在調用方不更改代碼的情況下更改當前參數。 只需確保所有類都可用,並進行測試。 有一些錯誤來源(錯別字)。
Guice實際上為您提供了很多配置功能。 假設我正確理解你,這里有一個簡單的例子,說明你可以在Guice中使用模塊中的提供者方法。
protected void configure() {
bind(TableWriter.class).to(PdfTableWriter.class);
}
@Provides
protected ListCreator provideListCreator(TableWriter tableWriter) {
ModularListCreator result = new ModularListCreator();
result.setTableWriter(tableWriter);
result.setSortDescending(true);
return result;
}
還有其他方法,包括使setSortDescending
使用綁定注釋:
@Inject public void setSortDescending(
@Named("sortDescending") boolean sortDescending)
然后綁定該屬性:
protected void configure() {
bind(TableWriter.class).to(PdfTableWriter.class);
bindConstant().annotatedWith(Names.named("sortDescending")).to(true);
bind(ListCreator.class).to(ModularListCreator.class);
}
對於CDI,請查看Seam Solder。 它增加了從xml文件輕松配置Managed Beans的功能。 從Seam和Weld團隊之間的密切關系來看,這種機制可能很有可能成為未來的JSR。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.