使用 Spring 配置的工厂返回新实例以供运行时调用
有没有一种简单的方法可以将 Spring IoC 用于工厂,在每次调用时返回一个新实例在运行时(在配置和上下文创建之后)?
我宁愿避免对 Spring 的强烈依赖,也避免每次调用 GetObject("myFacobj") ...我正在寻找类似方法指针注入的东西(就像我在编程时所做的那样) C 而不是 C#)。
也许可以使用 Spring 将委托注入到工厂调用中?
文章如何在 Spring 中注入 Predicate 和 Func 可能会带来一些启发.net 关于委托注入(Func<>,...),但如果可能的话,我更喜欢“Spring-Built-In-Solution”。
Is there an easy way to use Spring IoC for a factory which returns a new instance at every call during runtime (after configuration and context creation)?
I would prefer to avoid a strong dependency to Spring and also avoid calling GetObject("myFacobj") everytime... I am looking for something like a method pointer injection instead (as I would do when programming in C instead of C#).
Maybe it is possible to inject a delegate to a factory call using Spring?
Some inspiration might by the post How to inject Predicate and Func in Spring.net about injection of delegates (Func<>, ...) but I would prefer an "Spring-Built-In-Solution", if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring.NET支持方法注入 ,这可能很适合这里。
我在我的另一个答案中发布了一个关于如何使用方法的示例注入作为使用
context.GetObject(...)
的替代方案,这样您就不必依赖 di 容器。Spring.NET supports method injection, which could be a good fit here.
I've posted an example in this other answer of mine on how you might use method injection as an alternative to using
context.GetObject(...)
, so that you don't have to take a dependency the di container.