使用 IOC 容器时需要注意哪些事项(陷阱)?
5 回答
Trying not to fall too much for a Service Locator pattern, where some static wrapper to your IoC container provides you with instances of type xyz. While you may need it at times, ensuring that you do inject your dependencies to your consuming type will keep your code a lot cleaner and make the container much less intrusive.
如果你使用spring aop,就会有很多神奇的事情发生,如果某些事情不能正常工作,那么真的很难找出问题所在。
Complexity of configuration.
Keep an eye out up-front that the complexity you're getting with the various XML files and setup is worth the problem you're addressing. One example - in Apache HiveMind, the configuration of binding class instances to each other, and passing configuration information in can easily become more difficult to maintain, read, and understand then the equivalent Java would have required.
Troubleshooting can be harder in my experience. Not only are you working against interfaces, which means it can be difficult to identify the actual type of the instances used by looking at the code. You also move a lot of the wiring from compile time to runtime (which is one of the point of IoC of course, but it doesn't do wonders for finding problems).
在正确的时间处理正确的事情,否则会造成内存泄漏。