简体   繁体   English

没有反射的春天

[英]Spring without reflection

Is it possible to substitute default Spring Framework's way of creating and managing objects via reflections with other dependency injection tool (that would be faster, because would avoid reflections), while still holding on Spring's rich API?是否可以用其他依赖注入工具(这会更快,因为会避免反射)来替代默认的 Spring Framework 通过反射创建和管理对象的方式,同时仍然保留 Spring 的丰富 API?

For example, I would like to have beans created by Dagger 2 or Tiger or Feather that would still be able to interact with Spring Data/Social/MVC.例如,我希望 Dagger 2 或 Tiger 或 Feather 创建的 bean 仍然能够与 Spring Data/Social/MVC 交互。

https://github.com/google/dagger https://github.com/google/dagger

https://github.com/google/tiger https://github.com/google/tiger

https://github.com/zsoltherpai/feather https://github.com/zsoltherpai/feather

I know that someone is going to say "start worrying about performance when it will become problem" - well, I would say it's about time to start worrying about it right now.我知道有人会说“当性能成为问题时开始担心它” - 好吧,我想说现在是时候开始担心它了。

In my option, it would allow Spring to embrace FaaS (Function as a Service).在我的选择中,它将允许 Spring 接受 FaaS(功能即服务)。 FaaS jvm is going to be shut down after serving it's call, so You either keep it running (like regular server) and pay for literally every millisecond or some calls may be delayed even few seconds (to boot everything up). FaaS jvm 将在服务它的调用后关闭,因此您要么保持它运行(如常规服务器)并为几乎每毫秒支付一次费用,要么某些调用可能会延迟甚至几秒钟(以启动一切)。

I have found two projects, that are trying to use Spring in FaaS environment and are tackling this problem, but in my option it's easier to remove problem (reflections) that try to overcome it with hacks.我发现了两个项目,它们试图在 FaaS 环境中使用 Spring 并正在解决这个问题,但在我的选择中,更容易消除试图通过 hack 克服它的问题(反射)。

https://github.com/markfisher/spring-cloud-function https://github.com/markfisher/spring-cloud-function

https://github.com/kennyk65/spring-cloud-serverless https://github.com/kennyk65/spring-cloud-serverless

Or, maybe there is another way to solve this problem and efficiently use Spring in FaaS, that I am not aware of?或者,也许还有另一种方法可以解决这个问题并在 FaaS 中有效地使用 Spring,这是我不知道的?

Related question: Running Spring Boot on Amazon Lambda相关问题: 在 Amazon Lambda 上运行 Spring Boot

I have been trying to use minimal Spring Framework application (like 3-5 classes) and still it takes (sometimes) 5-15 seconds to handle first request (next are handled in 50-100ms), so minimizing isn't really working in this case.我一直在尝试使用最小的 Spring Framework 应用程序(如 3-5 个类),但仍然需要(有时)5-15 秒来处理第一个请求(下一个在 50-100 毫秒内处理),因此最小化并没有真正起作用这个案例。

I'm in the same boat, trying to finding a FaaS friendly DI framework for JVM.我在同一条船上,试图为 JVM 寻找一个 FaaS 友好的 DI 框架。 Since nothing can beat Spring ecosystem on JVM, it would be great if Spring had reflection-less mechanism and compile time DI.由于在 JVM 上没有什么可以打败 Spring 生态系统,如果 Spring 有无反射机制和编译时 DI,那就太好了。 I could not find much on that front though.不过,我在这方面找不到太多东西。

Micronaut solves the exact same problem and looks interesting. Micronaut解决了完全相同的问题,看起来很有趣。 It has an adapter layer for spring annotations.它有一个用于 spring 注释的适配器层。 Micronaut is purpose built for faster start-ups using compile time DI. Micronaut 专为使用编译时 DI 更快启动而构建。

Of course, this is good for newer applications but not for very large applications with lots of existing spring code-base.当然,这对于较新的应用程序是有好处的,但对于具有大量现有 Spring 代码库的大型应用程序则不然。

Today, spring-fu could be an option to create spring application reflection-less approach.今天, spring-fu可能是创建 spring 应用程序无反射方法的一个选项。

According to its documentation:根据其文档:

Spring Fu is an incubator for JaFu (Java DSL) and KoFu (Kotlin DSL) designed to configure Spring Boot explicitly with code in a declarative way with great discoverability thanks to auto-complete. Spring Fu 是 JaFu (Java DSL) 和 KoFu (Kotlin DSL) 的孵化器,旨在以声明式方式使用代码显式配置 Spring Boot,由于自动完成,具有很高的可发现性。 It provides fast startup (40% faster than regular auto-configuration on a minimal Spring MVC app), low memory consumption and is a good fit with GraalVM native thanks to its (almost) reflection-less approach.它提供快速启动(比最小 Spring MVC 应用程序上的常规自动配置快 40%)、低内存消耗,并且由于其(几乎)无反射方法非常适合 GraalVM 本机。

Note: spring-fu is not supposed to be used in production .注意: spring-fu 不应该用于生产

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

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