IoC 容器支持使用 Scala 命名/默认参数进行构造函数注入吗?

发布于 2024-10-21 14:07:39 字数 475 浏览 2 评论 0原文

如果我可以利用 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 in new 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没企图 2024-10-28 14:07:39

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

無心 2024-10-28 14:07:39

Scala 中的另一个依赖注入框架是 subcut

Another dependency injection framework in Scala is subcut

缺⑴份安定 2024-10-28 14:07:39

我考虑过使用FactoryBean-s来桥接Scala和Spring,但这意味着太多麻烦

我不确定我是否理解其中的复杂性。在 Scala 中实现 Spring FactoryBeans 实际上非常简单。检查这篇小文章 http://olegzk.blogspot.com /2011/07/implementing-springs-factorybean-in.html

I have considered using FactoryBean-s to bridge Scala and Spring, but that would mean too much hassle

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

萌︼了一个春 2024-10-28 14:07:39

我刚刚发布了 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文