如何解决Spring注入了@Configuration中指定的bean之外的bean
最近我在 Spring 中连接 bean 时犯了一个错误,导致了我无法复制的行为。当使用 @Value
中定义的另一个类型为 Stuff
的 bean 的值,而不是将源自 String
的属性注入 @Configuration
(请参阅下面的完整演示代码)应用程序已部署。
令我感到困惑的是,在本地运行(包括单元测试)时,一切都按预期工作,输出是 foo
而不是 kaboom
,并且这种“bean 交换”在部署时发生了而不是“没有符合条件的 bean”错误。
注释掉的行显示了我认为使配置类似于 the manual 中的配置的修复。
我的设置有什么问题?什么会使所示代码(即没有修复)使用 kaboom
String
而不是 foo
属性?
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
open class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
open class Config {
// ...beans of types other than String in original code...
@Bean
open fun beanBomb(): String {
return "kaboom"
}
@Bean
// fix:
// @Value("\${stuff}")
open fun beanStuff(stuff: String): Stuff {
return Stuff(stuff)
}
}
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
@Component
class Stuff(@Value("\${stuff}") val stuff: String)
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import javax.annotation.PostConstruct
@Component
class Init {
@Autowired
private lateinit var stuff: Stuff
@PostConstruct
fun init() {
println("stuff: " + stuff.stuff)
}
}
// application.properties
stuff=foo
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@TestPropertySource(properties = {"stuff=testFoo"})
class DemoApplicationTests {
@SpyBean
private Stuff stuff;
@Test
void test() {
assertEquals("testFoo",stuff.getStuff());
}
}
此外,应用修复后是否需要 @Value
中的 Stuff
注释?如果我取消注释修复,从 @Value
中删除 Stuff
并将以下注释添加到测试通过的测试类:
@ContextConfiguration(classes = {Config.class})
但是当我运行应用程序时,它会打印 kaboom
...
解决方法
您可以检查 bean 的创建顺序。如果 bean 是在我看来之前创建的,则 Spring IoC 容器按类型注入值,即 kaboom
并且由于默认情况下任何类型的 Bean 都是单例的,Stuff
的实例将不会进入即使用 @component
注释也能产生效果。
在您的测试中,您手动加载配置,其中注入的是 Stuff
中定义的 Config
bean,而不是使用 Stuff
注释的 @component
。
问题是注解需要放在参数上而不是函数上。
按照您的方式,Spring 正在寻找满足 String
类型的 bean,并且函数 String
生成了一个 beanBomb()
类型的 bean。如果您像这样移动注释,它应该消除歧义。
@Bean
open fun beanStuff(@Value("\${stuff}") stuff: String): Stuff {
return Stuff(stuff)
}
我想补充一点,拥有 String
类型的 bean 有点不寻常,但我想如果您不想使用属性/yaml 文件,它会允许您更改 {{1 }} 基于个人资料。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。