在Dev模式下运行Spring Boot应用会导致bean重复。

huangapple go评论54阅读模式
英文:

Running Spring Boot Application in Dev Mode causes beans to be duplicated

问题

我想在开发模式下运行我的Spring Boot应用程序:

我的主要(生产)应用程序:

fun main(args: Array<String>) {
    runApplication<MyApp>(args = args, init = {
        addInitializers(BeansInitializer())
    })
}

开发模式下的应用程序:

import com.myApp.main as prodMain

object MyApplicationTest {
    @JvmStatic
    fun main(args: Array<String>) {
        SpringApplication.from(::prodMain)
            .with(RedisContainerDevMode::class.java)
            .run(*args)
    }
}
@TestConfiguration
class RedisContainerDevMode {

    @Bean
    @ServiceConnection("redis")
    fun redis(): GenericContainer<*> =GenericContainer("redis:latest").withExposedPorts(6379)

}

除此之外,我有一个上下文初始化程序,测试时需要使用 @SpringBootTest

context:
  initializer:
    classes: com.myApp.BeansInitializer

这个初始化程序导致Bean定义被加载两次,并导致开发应用程序崩溃。

我如何同时实现以下两点:

  1. 使用 @SpringBootTest 运行测试,并从 BeansInitializer 初始化Bean
  2. 在开发模式下运行应用程序,避免重复加载Bean?
英文:

I want to run my Spring Boot application in the dev mode:

My main (prod app):

fun main(args: Array&lt;String&gt;) {
    runApplication&lt;MyApp&gt;(args = args, init = {
        addInitializers(BeansInitializer())
    })
}

App in the dev mode:

import com.myApp.main as prodMain

object MyApplicationTest {
    @JvmStatic
    fun main(args: Array&lt;String&gt;) {
        SpringApplication.from(::prodMain)
            .with(RedisContainerDevMode::class.java)
            .run(*args)
    }
}
@TestConfiguration
class RedisContainerDevMode {

    @Bean
    @ServiceConnection(&quot;redis&quot;)
    fun redis(): GenericContainer&lt;*&gt; =GenericContainer(&quot;redis:latest&quot;).withExposedPorts(6379)

}

Apart from this, I have a context initializer that is needed for tests using @SpringBootTest

context:
  initializer:
    classes: com.myApp.BeansInitializer

This initializer causes Bean definitions to be loaded twice and crashes the development app.

How I can simultaneously have:

  1. Run tests using @SpringBootTest and have beans initialized from BeansInitializer
  2. Be able to run the app in the dev mode without duplicated beans?

答案1

得分: 0

我找到的解决方案是在测试中从 application.yml 中移除初始化程序,并将其移到 @SpringBootTest 注解中。

@SpringBootTest(properties = [&quot;context.initializer.classes=com.myApp.BeansInitializer&quot;])

这适用于集成测试和运行使用 Testcontainers 的 Spring Boot 应用程序。

英文:

The solution I found is to remove the initializer from application.yml in tests and move it to the @SpringBootTest annotation.

@SpringBootTest(properties = [&quot;context.initializer.classes=com.myApp.BeansInitializer&quot;])

This makes a perfect fit for both integration tests and running Testcontainers powered Spring Boot app

huangapple
  • 本文由 发表于 2023年6月22日 19:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定