简体   繁体   English

是否需要使用Scala蛋糕模式进行重新编译以进行依赖项注入?

[英]Is recompilation required using the Scala cake pattern for dependency injection?

Using Spring dependency injection it is possible to swap out a dependency at run time by tweaking the XML file. 使用Spring依赖注入,可以通过调整XML文件在运行时交换依赖。

I don't think this is possible using the Scala cake pattern dependency injection since the dependencies are wired at compile time and to change the dependencies would require re-compilation ? 我不认为使用Scala蛋糕模式依赖项注入是不可能的,因为依赖项是在编译时连接的,要更改依赖项,需要重新编译? If so is this an advantage that Spring dependency injection has over using the Scala cake pattern for dependency injection ? 如果是这样,那么Spring依赖注入相对于使用Scala Cake模式进行依赖注入是否具有优势?

Yes using the cake pattern you will need to recompile. 是的,使用蛋糕模式需要重新编译。 The whole point of the cake pattern is to do compile time dependency injection :) With spring you will return to "classic" (in java world) runtime dependency injection, which is done through reflection. 蛋糕模式的全部要点是进行编译时依赖注入:)使用spring时,您将返回“经典”(在Java世界中)运行时依赖注入,这是通过反射完成的。

Using runtime dependency injection, you might be able to swap out a dependency at runtime either by restarting the whole container or by using specific modules like jrebel to update only part of the context. 使用运行时依赖项注入,您可以通过重新启动整个容器或使用特定模块(如jrebel)仅更新部分上下文,从而在运行时交换依赖项。 In exchange you pay the price at application startup as your container has to parse the XML, instanciate all the objects and wire them up. 作为交换,您需要在应用程序启动时付出代价,因为容器必须解析XML,实例化所有对象并将它们连接起来。 In the case of spring the wiring can be fairly complex requiring multiple passes to complete. 在弹簧的情况下,接线可能相当复杂,需要多次通过才能完成。 Usually you don't want to swap out dependencies at runtime in production code (there are valid use cases for this but I have rarely encountered them). 通常,您不想在运行时在生产代码中交换依赖项(有有效的用例,但我很少遇到它们)。

On the other hand, the cake pattern is validated at compile time, the wiring is necessarily predetermined (forget about @PostConstruct). 另一方面,蛋糕模式是在编译时验证的,接线必须预先确定(忘记@PostConstruct)。 Avoiding complex wiring cycles is actually a good thing :) You will also avoid loading and parsing XML files (though in my understanding the current preferred configuration method is using code not XML). 避免复杂的接线循环实际上是一件好事:)您还将避免加载和解析XML文件(尽管据我了解,当前的首选配置方法是使用代码而不是XML)。 Errors in the wiring will appear sooner in cake giving you a quicker feedback. 接线错误会更快地出现在蛋糕上,从而为您提供更快的反馈。

Last but not least, there are other DI mechanisms available in functionnal programming languages (such as scala) such as using an IO or a Reader Monad. 最后但并非最不重要的一点是,在函数式编程语言(例如scala)中还有其他DI机制可用,例如使用IO或Reader Monad。 For more about such mechanisms you can start at : http://blog.originate.com/blog/2013/10/21/reader-monad-for-dependency-injection/ http://fr.slideshare.net/debasishg/dependency-injection-in-scala-beyond-the-cake-pattern or Using Reader Monad for Dependency Injection 有关此类机制的更多信息,请访问: http : //blog.originate.com/blog/2013/10/21/reader-monad-for-dependency-injection/ http://fr.slideshare.net/debasishg/超出蛋糕模式 的标量依赖注入,使用Reader Monad进行依赖注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM