如何从枚举单例的工厂方法创建 spring bean

Sam*_*age 0 java spring

我试图在这里弹性化示例代码

http://xeiam.com/xchange_examplecode.jsp

  public static void main(String[] args) {

    // Demonstrate the public market data service
    // Use the factory to get the version 2 MtGox exchange API using default settings
    Exchange mtGoxExchange = ExchangeFactory.INSTANCE.createExchange(MtGoxExchange.class.getName());

    // Interested in the public market data feed (no authentication)
    PollingMarketDataService marketDataService = mtGoxExchange.getPollingMarketDataService();
Run Code Online (Sandbox Code Playgroud)

基本上我希望将 PollingMarketDataService 或 Exchange 作为 spring bean 注入。

然而上面的 ExchangeFactory 是一个枚举,当我尝试这个时:

<beans:bean id="exchangeFactory" class="com.xeiam.xchange.ExchangeFactory" factory-method="valueOf">
    <beans:constructor-arg value="INSTANCE"/>
</beans:bean>

<beans:bean id="mtGoxExchange" factory-bean="exchangeFactory" factory-method="createExchange">
    <beans:constructor-arg value="com.xeiam.xchange.mtgox.v2.MtGoxExchange"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

ExchangeFactory 为空。

Juk*_*kka 5

这应该有效:

<util:constant id="exchangeFactory" static-field="com.xeiam.xchange.ExchangeFactory.INSTANCE" />

<bean id="mtGoxExchange" factory-bean="exchangeFactory" factory-method="createExchange">
    <constructor-arg value="com.xeiam.xchange.mtgox.v2.MtGoxExchange" />
</bean>
Run Code Online (Sandbox Code Playgroud)

试一试。