SpringBoot-@配置创建一个单例bean
在SpringBoot应用程序中,当在用@Configuration注释的类中创建bean时,它是单例的吗?或者它是为每个请求创建的。例如,在下面的代码中,clientBean是singleton吗SpringBoot-@配置创建一个单例bean,spring,spring-boot,Spring,Spring Boot,在SpringBoot应用程序中,当在用@Configuration注释的类中创建bean时,它是单例的吗?或者它是为每个请求创建的。例如,在下面的代码中,clientBean是singleton吗 @Configuration(name = "clientBean") class StarupConfiguration { @Bean fun ApiServiceClient(): IApiServiceClient { return new ApiServiceClien
@Configuration(name = "clientBean")
class StarupConfiguration {
@Bean
fun ApiServiceClient(): IApiServiceClient {
return new ApiServiceClient();
}
}
是单身汉。查看以了解如何修改该行为。它还确认singleton是默认范围。默认情况下,
@Bean
生成一个singletonBean,由Spring容器管理
如果要修改此行为,可以使用@Scope
标记该方法,以从单例切换到所需的任何其他作用域
@Bean
@范围(“原型”)
公共MyBean MyBean(){
返回新的MyBean();
}
看