【发布时间】:2016-03-02 12:08:43
【问题描述】:
我想在 Guice 中将接口与 Impl 绑定时传递构造函数参数。
有几个解决方案,但我没有这些接口/Impl 的代码访问权限。 它们是由其他团队开发的,我已将它们包含在我的项目中。
- @Named/@Assisted - 两者都需要更改 Impl 的源代码(构造函数)以包含这些注释。 (我无权访问此代码)
- 实现 Provider - 通过加载所需的参数返回该 Impl 的实例。 这行得通。 但问题是我有 200 多个这样的现有 DI,我必须编写 200 多个提供程序。
目前我们正在使用 Spring DI,并且正在迁移到 Guice。 所以我需要定义类似
<bean name="clientWrapper" class="com.wrapper.client.ClientWrapper">
<constructor-arg index="0" value="${server.ip}" />
<constructor-arg index="1" value="${server.port}" />
</bean>
在吉斯。但是传递那些构造函数参数。
bind(IClientWrapper.class).to(ClientWrapper.class);
如何在不使用 Providers 的情况下实现这一点?
【问题讨论】:
标签: java spring dependency-injection guice