简体   繁体   English

Spring 是否在 Kotlin object 中自动接线?

[英]Is Spring autowired in Kotlin object redundant?

In the Spring framework, by default, bean scope is Singleton.在 Spring 框架中,默认情况下,bean scope 为 Singleton。 But with Kotlin, we can easily create a Singleton object.但是使用 Kotlin,我们可以轻松创建 Singleton object。 Just need to do只需要做

object SimpleService {
  fun businessFunction() = ...
}

If we try to use Spring to manage the scope of a Kotlin object , would it be some kind of redundancy?如果我们尝试使用 Spring 来管理 Kotlin ZA8CFDE6331BD59EB2AC966F8911C4B 的object会是某种冗余吗? Because I should only need to do...因为我应该只需要做...

  • Rather than create a class and have Spring manage objects, I just need to create a Kotlin object (singleton).而不是创建一个 class 并让 Spring 管理对象,我只需要创建一个 Kotlin object

  • If we don't want to violate the interface segregation principle , we will create an interface and a Service just needs to implement it.如果我们不想违反接口隔离原则,我们将创建一个接口,一个Service只需要实现它。 And only need to register to Spring something like: @Bean IMyService = SimpleSerice并且只需要注册到 Spring 类似: @Bean IMyService = SimpleSerice

The only one thing Spring can help us with Kotlin is Spring Data (Create Repository Interface without implementation). Spring 可以帮助我们处理 Kotlin 的唯一一件事是 Spring 数据(创建没有实现的存储库接口)。 But I think we only need to integrate with some open source for it (or create new one)但我认为我们只需要为它集成一些开源(或创建新的)

I'm concerned that Spring might be totally redundant and heavy for Kotlin.我担心 Spring 对于 Kotlin 来说可能是完全冗余和沉重的。

Spring Beans have a different purpose than Kotlin objects and are not comparable per se. Spring Bean 与 Kotlin 对象具有不同的用途,并且本身不可比较。

With Spring beans, you get (environment-specific) configuration, dependency injection, ... .使用 Spring bean,您可以获得(特定于环境的)配置、依赖注入,...。 You can actually create multiple beans of the same type with different values.您实际上可以创建多个具有不同值的相同类型的 bean。

When it comes to testing, you can use slices or tailor tests yourself to only instantiate the beans you absolutely need for the test.在测试方面,您可以自己使用切片或定制测试,以仅实例化您绝对需要进行测试的 bean。 Furthermore, you can mock them.此外,您可以模拟它们。

TBOMK, kotlin object expressions are initialized when the containing file is loaded - which might not be too transparent in larger projects. TBOMK、kotlin object 表达式在加载包含文件时被初始化——这在较大的项目中可能不太透明。 This has the side effect that you might not be able to control it in tests.这具有您可能无法在测试中控制它的副作用。 Also, configuration and injections have to be achieved differently.此外,必须以不同方式实现配置和注入。 Never tried mocking... .从未尝试过 mocking ......

I use Kotlin with Spring and have various Spring beans in action.我使用 Kotlin 和 Spring 并有各种 Spring 豆子在行动。 I use companion objects and object expressions whenever I have a very static context with fast initialisation.每当我有一个快速初始化的非常 static 上下文时,我都会使用伴随对象和 object 表达式。 So these objects serve more static concerns while spring beans serve dynamic ones.因此,这些对象服务于更多 static 关注点,而 spring bean 服务于动态关注点。

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

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