IoC 容器支持使用 Scala 命名/默认参数进行构造函数注入吗?
如果我可以利用 Scala 2.8 的命名和默认参数功能,我更喜欢使用构造函数注入而不是 JavaBean 属性注入。是否存在支持该功能或可以轻松扩展的 IoC 容器? (所需的信息位于类的 scala.reflect.ScalaSignature 注释中的运行时。)
我对 IoC 容器也有一些基本的(?)期望:
- 自动装配(按目标类/特征或注释,一对一和一对多)
- 显式注入(显式连接)没有太多麻烦(就像 Guice 在那里很弱)。就像
user
是以这种方式注入到new ConnectionPool(user="test")
中一样。 - 用于关闭时清理的生命周期回调(以正确的顺序)
Spring 显然可以做到这些,但它不支持命名参数。我考虑过使用 FactoryBean-s 来桥接 Scala 和 Spring,但据我所知,这意味着太多麻烦(样板文件或代码生成)。
I would prefer using constructor injection over JavaBean property injection if I could utilize the named and default arguments feature of Scala 2.8. Any IoC-containers exists that supports that or could be easily extended to do so? (The required information is there on runtime in the scala.reflect.ScalaSignature
annotation of the class.)
I also have some basic(?) expectations from the IoC container:
- Auto-wiring (by target class/trait or annotation, both one-to-one and one-to-many)
- Explicit injection (explicit wiring) without much hassle (like Guice is weak there). Like
user
is injected that way innew ConnectionPool(user="test")
. - Life-cycle callback for cleanup on shutdown (in the proper order)
Spring can do these, obviosuly, but it doesn't support named parameters. I have considered using FactoryBean
-s to bridge Scala and Spring, but that would mean too much hassle (boilerplate or code generation), as far as I see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
A 部分
我有一个正在开发的反射库,它可以解析 Scala 签名,目前能够解析命名参数: https://github.com/scalaj/scalaj-reflect
不幸的是,我还没有将其绑定到 Java 反射中以便能够调用方法,也没有添加逻辑来解析默认值(尽管这应该是微不足道的)。这两个功能在我的待办事项列表中都非常重要:)
这本身不是一个 IoC 容器,但它是我的另一个项目的先决条件:https://github.com/scalaj/scalaj-spring。当我明显意识到,除非我有了基于签名的反射,否则我将无法取得任何有价值的进一步进展,所以 scalaj-spring 的工作就停止了。
B 部分
无论如何,所有这些东西都是为有事业心的人准备的。那些别无选择只能将闪亮的新 Scala 代码集成到一些庞大的遗留系统中的人...如果这不是您的用例,那么您可以直接在 Scala 中执行 Scala DI。
Lift 横幅下提供了 DI 支持: http://www.assembla.com/wiki/ show/liftweb/Dependency_Injection
您还应该寻找对蛋糕模式的引用
PART A
I have a work-in-progress reflection library that parses the Scala signature and is currently able to resolve named parameters: https://github.com/scalaj/scalaj-reflect
Unfortunately, I haven't yet tied it back into Java reflection to be able to invoke methods, nor have I added the logic to resolve default values (though this should be trivial). Both features are very high on my to-do list :)
This isn't an IoC container per-se, but it's a pre-requisite for another project of mine: https://github.com/scalaj/scalaj-spring. Work on scalaj-spring stopped when it became blindingly obvious that I wouldn't be able to make any worthwhile further progress until I had signature-based reflection in place.
PART B
All of that stuff is intended for big enterprisey people anyway. Those with no choice but to integrate their shiny new Scala code into some hulking legacy system... If that's not your use case, then you can just do Scala DI directly inside Scala.
There's DI support provided under the Lift banner: http://www.assembla.com/wiki/show/liftweb/Dependency_Injection
You should also hunt around for references to the cake pattern
Scala 中的另一个依赖注入框架是 subcut
Another dependency injection framework in Scala is subcut
我不确定我是否理解其中的复杂性。在 Scala 中实现 Spring FactoryBeans 实际上非常简单。检查这篇小文章 http://olegzk.blogspot.com /2011/07/implementing-springs-factorybean-in.html
I am not sure I understand the complexity. Its actually quite simple to implement Spring FactoryBeans in Scala. Check this little write-up http://olegzk.blogspot.com/2011/07/implementing-springs-factorybean-in.html
我刚刚发布了 Sindi,一个 Scala 编程语言的 IoC 容器。
http://aloiscochard.github.com/sindi/
I've just released Sindi, an IoC container for the Scala programming language.
http://aloiscochard.github.com/sindi/