简体   繁体   English

Java配置和依赖注入(类似于Springs IoC vs. Weld / Guice)

[英]Java configuration and dependency injection (akin to Springs IoC vs. Weld / Guice)

Lets say I have a class ListCreator which I want to configure. 假设我有一个我想配置的ListCreator类。 I want to be able to tell it the sort order and the how to output my table. 我希望能够告诉它排序顺序以及如何输出我的表。 Therefore I have the boolean sortDescending property and the TableWriter interface which is implemented by PdfTableWriter (but also by XlsTableWriter ). 因此,我有TableWriter sortDescending属性和TableWriter接口,它由PdfTableWriter (也由XlsTableWriter )实现。 In this example I think configuration and DI go hand in hand. 在这个例子中,我认为配置和DI是相辅相成的。 I would like to write something like this Spring (pseudo) example: 我想写一些类似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>

Now Spring can do this, but it seems like Weld & Guice can not. 现在Spring可以做到这一点,但似乎Weld&Guice不能。 Weld for example lets you choose alternatives in the beans.xml, but only for the whole application. 例如,Weld允许您在beans.xml中选择备选方案,但仅适用于整个应用程序。 What if I want to have one ListCreator for PDFs and another one for XLS at the same time? 如果我想同时拥有一个ListCreator用于PDF而另一个用于XLS,该怎么办?

I do not get the scope of Weld and Guice at the moment, as they don't seem to allow much of a configuration. 我目前还没有得到Weld和Guice的范围,因为它们似乎不允许太多的配置。 The seem to only alleviate the need to write new or to implement your own factories. 这似乎只是减轻了编写new或实现自己的工厂的需要。 EJB injection does the same for example, which is nice, but where is the whole configuration part (choosing which instance with what parameters I actually want where). 例如,EJB注入做的相同,这很好,但是整个配置部分在哪里(选择哪个实例具有我实际想要的参数)。

Coming to the real point: I do not want to use Spring as it seems to be overhead. 来到真正的问题:我不想使用Spring,因为它似乎是开销。 I much rather use something clean and small at best specified by a JSR. 我更喜欢使用JSR指定的干净小巧的东西。 Any suggestions? 有什么建议么?

InPUT offers a way if you want a flexible description-based solution. 如果您需要灵活的基于描述的解决方案, InPUT提供了一种方法。 I created the whole example, and added it to the example section . 我创建了整个示例,并将其添加到示例部分

The code is minimal with: 代码很小,包括:

Design config = new Design("config.xml");
ListCreator creator = config.getValue("ListCreator");

assuming you have a config.xml InPUT design which contains the settings in InPUT syntax: 假设您有一个config.xml InPUT 设计 ,其中包含InPUT语法中的设置:

<SValue id="ListCreator">
    <NValue id="SortDescending" value="false"/>
    <SValue id="TableWriter" value="Xls"/>
</SValue>

For this to work, you have to define the design space as follows: 为此,您必须按如下方式定义设计空间

<SParam id="ListCreator">
    <NParam id="SortDescending" type="boolean" />
    <SParam id="TableWriter">
        <SChoice id="Xls"/>
        <SChoice id="Pdf"/>
    </SParam>
</SParam>

You now tailor the programming language independent design space to your Java implementation in a code mapping : 您现在可以在代码映射中为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"/>

From here, extend and customize at free will without touching the code. 从这里,免费扩展和自定义,无需触摸代码。 You mention the case with multiple ListCreator instances. 您提到了多个ListCreator实例的情况。 You would have to make 3 changes: 您必须进行3次更改:

1) design space: 1)设计空间:

<SValue id="ListCreator" type="[]">

2) design (for example): 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) Be prepared to receive an array instead (code): 3)准备好接收一个数组(代码):

ListCreator[] creators = config.getValue("ListCreator");

You decide the number and alternatives in the descriptor; 您可以决定描述符中的数字和替代项; the entries arrive in the defined order. 条目以定义的顺序到达。 This works similar for multiple dimensions (eg "[][][]"). 这适用于多个维度(例如“[] [] []”)。 You can add alternative table writers with further parameters in the future or change the current parameters without code changes on the caller side. 您可以在将来添加具有更多参数的替代表编写器,或者在调用方不更改代码的情况下更改当前参数。 Just make sure the classes are all available, and to test it. 只需确保所有类都可用,并进行测试。 There are some sources of error (typos). 有一些错误来源(错别字)。

Guice actually gives you a lot of power for configuration. Guice实际上为您提供了很多配置功能。 Assuming I'm understanding you correctly, here's a simple example of one way you could do this in Guice, using a provider method in a module. 假设我正确理解你,这里有一个简单的例子,说明你可以在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;
}

There are other ways too, including making setSortDescending use a binding annotation: 还有其他方法,包括使setSortDescending使用绑定注释:

@Inject public void setSortDescending(
    @Named("sortDescending") boolean sortDescending)

and then binding that property: 然后绑定该属性:

protected void configure() {
  bind(TableWriter.class).to(PdfTableWriter.class);
  bindConstant().annotatedWith(Names.named("sortDescending")).to(true);
  bind(ListCreator.class).to(ModularListCreator.class);
}

For CDI, checkout Seam Solder. 对于CDI,请查看Seam Solder。 It adds the ability to easily configure Managed Beans from an xml file. 它增加了从xml文件轻松配置Managed Beans的功能。 Judging by the close relationship between the Seam and Weld teams, this mechanism probably has a good chance of making it into a future JSR. 从Seam和Weld团队之间的密切关系来看,这种机制可能很有可能成为未来的JSR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM