我一直在学习 Guice。我看到有按需注入(inject)here .
我想知道它的用途和一些例子。我有一个场景,我从conf 文件中读取一组属性。那里没有注入(inject)。后来我想将这些属性中的配置类的相同实例注入(inject)到其他类中。
class Props {
//set of properties read from a config file to this class
}
Props props = readProperties(); // instance of this class having all the properties but not put into injection container
稍后在连接类中我想使用它的注入(inject)
@Inject
public Connection(Props props) {
this.props = props;
}
这种情况下是否可以使用 Guice 的按需注入(inject)?我还使用 Play 框架的 conf 文件来加载我的模块文件。 play.modules.enabled += com.example.mymodule
最佳答案
如果您想使用完全相同的配置实例(Props 类),您可以将其实例绑定(bind)为 singleton与 provider binding 。这当然不是唯一的解决方案,但对我来说很有意义。
这是一个例子:
定义提供者:
public class PropsProvider implements Provider<Props>
{
@Override
public Props get()
{
...read and return Props here...
}
}
在单例范围内使用提供者绑定(bind):
bind(Props.class).toProvider(PropsProvider.class).in(Singleton.class);
注入(inject)您的配置:
@Inject
public Connection(Props props) {
this.props = props;
}
您可以阅读文档:
Singletons are most useful for:
- stateful objects, such as configuration or counters
- objects that are expensive to construct or lookup
- objects that tie up resources, such as a database connection pool.
也许您的配置对象符合第一个和第二个条件。我会避免从模块内读取配置。看看为什么 here .
我在一些单元测试用例中使用了按需注入(inject),我想在被测组件中注入(inject)模拟依赖项,并且使用了字段注入(inject)(这就是我尝试避免字段注入(inject)的原因:-))并且我更喜欢不使用InjectMocks由于某些原因。
这是一个示例:
组件:
class SomeComponent
{
@Inject
Dependency dep;
void doWork()
{
//use dep here
}
}
测试本身:
@RunWith(MockitoJUnitRunner.class)
public class SomeComponentTest
{
@Mock
private Dependency mockDependency;
private SomeComponent componentToTest;
@Before
public void setUp() throws Exception
{
componentToTest = new SomeComponent();
Injector injector = Guice.createInjector(new AbstractNamingModule()
{
@Override
protected void configure()
{
bind(Dependency.class).toInstance(mockDependency);
}
});
injector.injectMembers(componentToTest);
}
@Test
public void test()
{
//test the component and/or proper interaction with the dependency
}
}
关于java-8 - Guice按需注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347663/