使用 Guice 的逆向工程项目策略?
我试图理解依赖注入,但并没有完全理解它,除非我设法理解它使得理解别人的代码变得困难。 :'(
无论如何,我不知道如何简要描述我的问题,但我会尝试。我目前是唯一一个从事 Java 项目的编码员,该项目由数十名孤独者在大约六年的时间内完成。它大量使用了Google 的 Guice 库。我应该采用一些现有代码并以不同的方式实现它;使用现有的密码身份验证方法,而不是将其应用于 JMenu 中的每个 JMenuItem,而是将其应用于整个 JMenu,这样如果输入错误的密码或未输入密码,所有 JMenuItem 都被禁用,如果密码错误,则不会发生这种情况,这使我相信问题出在 if 语句中,它本身就是一长串依赖项:if (!ViewScanApp.getApplication().getHistoryManager().isAuthenticated())
我追溯这一点,发现 HistoryManager
类是一个接口,并且我的路似乎就此终结;那里没有代码,也没有引用任何其他类。通过随机探索项目中的 100 多个类,我找到了路径的尽头,但我似乎无法将它们联系起来。我找不到在该堆栈的另一端找到的第一个类 AccessManagerImpl
的调用位置。
我可以使用可能适用于这种情况的依赖注入的解释。太感谢了!
I have tried to understand dependency injection and not quite gotten it, except I have managed to pick up the understanding that it makes it hard to understand somebody else's code. :'(
Anyway, I'm not sure how to briefly describe my problem but I will try. I am currently the sole coder working on a Java project that's been worked on by dozens of loners over about six years. It makes heavy use of Google's Guice library. I'm supposed to take some existing code and implement it differently; specifically, use the existing methods for password authentication and, instead of applying it to each JMenuItem in a JMenu, apply it to the whole JMenu, so that if the wrong password or no password is entered, all JMenuItems are disabled. This doesn't happen if the password is wrong, leading me to believe the problem is in the if statement, which is a long string of dependencies in itself:if (!ViewScanApp.getApplication().getHistoryManager().isAuthenticated())
I trace my way back through this, to find that the HistoryManager
class is an interface, and there my path seems to die; there's no code there, and it doesn't make a reference to any other class. I have found the end of the path through random exploration of the 100-odd classes in the project, but I can't quite seem to connect them. I cannot find where the first class I can find on the other end of this stack, AccessManagerImpl
, gets called.
I could use an explanation of dependency injection that might be applicable to this situation. Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
HistoryManager
接口上没有@ImplementedBy
注释,您需要检查负责绑定此类型的 GuiceModule
。在 Eclipse 中,有一个命令可以查找类的出现情况。我敢打赌 Netbeans 也有类似的东西。使用它来查找
HistoryManager
的出现情况。其中至少一个应该出现在实现com.google.inject.Module
(或扩展AbstractModule
)的类中。您可能会看到类似的内容或者,如果您喜欢快速而肮脏的经验主义,您可以放入
println()
:无论您如何找到它,
HistoryManagerImpl
类都是您想要寻找踪迹的地方。我还没用过,但Guice 绘图工具可能是也有帮助。
Assuming there's no
@ImplementedBy
annotation on theHistoryManager
interface, you'll need to examine the GuiceModule
that is responsible for binding this type.In Eclipse, there is a command to look for occurrences of a class. I'll bet that Netbeans has something similar. Use it to look for occurrences of
HistoryManager
. At least one of these should occur in a class that implementscom.google.inject.Module
(or extendsAbstractModule
). You'll likely see something likeOr, if you like quick-and-dirty empiricism, you can throw in a
println()
:However you locate it,
HistoryManagerImpl
class is where you'll want to pick up the trail.I haven't used it, but the Guice graphing tool might be helpful too.
启动调试器。它将引导您完成实现该接口的确切类(假设您有它的源代码)
Fire up a debugger. It will walk you through the exact class that is implementing that interface (assuming you have the source code to it)
每当您在 Eclipse 中有一个用 Guice 注入的接口定义时,不要使用 F3 转到您将执行的定义(如果它是一个类),然后使用 Ctrl-T 在该接口的实现中进行选择。
如果您有多个模块可供选择,那么您需要打印出模块绑定,以便您知道选择哪一个。不幸的是 Eclipse 还不理解注入。
Whenever you have an interface definition in Eclipse which is injected with Guice, instead of using F3 to go to the defintion which you would do if it was a class, then use Ctrl-T to choose among the implementations of that interface.
If you have more than one to choose from, then you need the module bindings printed out so you know which one to pick. Unfortunately Eclipse doesn't understand injection yet.