我一直在學習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)之類的東西。這聽起來很骯髒:-) –