我一直在学习Guice。我看到有一个按需注入here。Guice按需注射
我想知道它的用途和一些例子。我有一个场景,我从conf文件中读取一组属性。那里没有注射。后来我想将这些属性的配置类的相同实例注入其他类。
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
public Connection(Props props) {
this.props = props;
}
是否可以使用按需喷射吉斯在这种情况下?另外我使用Play框架的conf文件来加载我的模块文件。 play.modules.enabled + = com.example.mymodule
嗨,我有一个查询。我本来希望使用Provider,但是我从之前得到了对Props的参考,现在我无法读取它。所以我只是想知道我是否可以在获得它时注入参考,并在任何地方使用Injector。 –
根据您的情况有多种解决方案。不幸的是我不熟悉Play FW。但是......上面的单例是懒惰的(直到需要时才会被实例化) - 这意味着你仍然可以使用具有内部状态的提供者 - 例如你可以在你的模块定义中使用相同的提供者实例,并且在你阅读道具的地方只需在同一个共享实例上调用诸如provider.setProps(props)之类的东西。这听起来很肮脏:-) –