Java Collections API 上的依赖注入
我正在编写一个 Java 组件,它将在一些大数据上完成相当繁重的工作。因此,显然,效率(速度和内存)至关重要。
我也刚刚开始第一次使用 IoC 和依赖注入框架(例如 Spring AOP、Google Guice 等),并且想知道它们是否可以帮助我。
我想做的是这样的(在我的代码中):
List<MyData> oMyData = new List<MyData>();
然后,在一些 XML 配置文件(或者框架配置注入)中,我会指定每次创建 List 实例时注入,比如说,一个 ArrayList
这样,如果以后我决定使用不同的实现,甚至是比 Java Collections 提供的更有效地适合我的应用程序的本地实现,我只需要更改注入的类。不需要进行其他修改,并且我的代码运行效率会更高。
这是一个可能的解决方案,还是这只是一个伟大的梦想?同样,因为我对 IoC 框架很陌生,所以很难判断注入将在哪些对象上起作用,在注入期间是否可以保留泛型,以及各种其他复杂的细节,我似乎无法在其他任何地方找到答案。
预先感谢您的任何见解或建议!
I'm writing a Java component that will be doig pretty heavy-duty work on some big data. Obviously, therefore, efficiency (both speed and memory) is paramount.
I'm also just starting to work with IoC and dependency injection frameworks for the first time (such Spring AOP, Google Guice, etc.), and was wondering if they could help me out at all.
What I'd like to do is something like this (all throughout my code):
List<MyData> oMyData = new List<MyData>();
Then, in some XML config file (or however the framework configures injections), I would specify that every time an instance of List is created, to inject, say, an ArrayList<MyData>
as its implementation.
This way, if, down the road, I decide to use a different implementation, or even something homegrown that suits my application more efficiently than anything provided by Java Collections, I only need to change the class that gets injected. No other modifications would be necessary, and my code will run that much more efficiently.
Is this is a possible solution, or is this just big dreaming? Again, because I'm so new to IoC frameworks it's tough to tell what objects injections will work on, whether generics can be preserved during injection, and all sorts of other complicating details I can't seem to find answers to anywhere else.
Thanks in advance for any insight or recommendations!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这听起来不像 IOC 容器的经典场景,但您可以使用工厂方法。
在您的应用程序中使用此工厂方法。现在,如果您想使用不同的列表实现,只需更改此方法即可。妈妈看,不需要容器。
需要明确的是:IOC 是一个很棒的概念。但我不认为你所说的是国际奥委会的经典场景。
This does not sound like a classic scenario for an IOC container, but you could use a factory method.
Use this factory method all over your app. Now if you want to use a different list implementation, just change this method. Look mom, no container needed.
Just to be clear: IOC is an awesome concept. But I don't think what you are talking about is a classic scenario for IOC.
这是您要找的吗?
这是开箱即用的弹簧......
Is this what you're looking for ?
This comes out-of-the box with spring...
您的目标很有可能,而且实际上是一个很好的推荐实践,也称为依赖注入。
您可以使用 Spring 来实现此目的。当然,注入对象的确切语法将与您此处显示的有很大不同。
例如,在 Spring 中,您可以编写
List; oMyData =factory.getBean("MyData")
在一个单独的配置文件中,您准确指定什么是
MyData
,这将被注入。What you are aiming is very much possible , and in fact a good recommended practice , also called as Dependency Injection.
You can use Spring for this. Ofcourse the exact syntax of injecting the object is going to be much different than what you show here.
IN Spring , for example , you would write
List<MyData> oMyData = factory.getBean("MyData")
IN a separate config file , you specify exactly what is
MyData
, and this will get injected.正如之前所说,您的案例描述对于 DI 解决方案来说有些不传统。尤其是以这种方式调整性能有点不合适。我怀疑要在这方面真正取得成功,您甚至需要针对不同注入位置的多个实现。如果您想这样做,我建议使用 Silk DI。它允许在不同情况下绑定到不同的
List
实现 http:// /www.silkdi.com/userguide/binds.html#targeting 只需一些绑定。As said before your case description is somewhat unconventional for a DI solution. Especially to tune performance that way seams somewhat inappropriate. I suspect to really succeed on this you would even need more than one implementation for different places of injection. If you want to go for that I recommend to use Silk DI. It allows to binds to different
List
implementations under different circumstances http://www.silkdi.com/userguide/binds.html#targeting with just a few bindings.