Sarav
Sarav

Reputation: 265

SpringBoot - @Configuration creates a singleton bean

In SpringBoot Application, when a bean is created inside a class annotated with @Configuration, is it singleton? Or is it created for each request. For example, in the below code, Is clientBean singleton?

@Configuration(name = "clientBean")
class StarupConfiguration {
 @Bean
 fun ApiServiceClient(): IApiServiceClient {
        return new ApiServiceClient();
 }
}

Upvotes: 1

Views: 4196

Answers (2)

Matheus
Matheus

Reputation: 3370

By default, the @Bean produces a singleton bean to be managed by the Spring Container.

If you want to modify this behaviour, you can mark the method with @Scope to switch from singleton to any other scope you want.

 @Bean
 @Scope("prototype")
 public MyBean myBean() {
     return new MyBean();
 }

See here.

Upvotes: 2

user2959589
user2959589

Reputation: 617

It's a singleton. Look at the @Scope annotation documentation to see how to modify that behavior. It also confirms that singleton is the default scope.

Upvotes: 0

Related Questions