Home > Software design >  Spring. Config classes with @Configuration annotation. Is it right to have more than one config clas
Spring. Config classes with @Configuration annotation. Is it right to have more than one config clas

Time:11-23

I'm asking about the current standard. Is it ok to have one class for security config, another for Swagger, and so on? Having more than one class annotated with @Configuration has any impact on performance?

CodePudding user response:

You can have as much @Configuration classes as you wish and I would recommend to separate beans by their realm - for example SecurityConfig, PersistenceConfig etc as you usually need to declare a lot of beans and this way you keep your code clean, readable and therefore easier to maintain.

As for the performance impact it should not be something to be worried about as additional class impact performance just by it's loading time and that is not a big cost in comparison to benefits coming from multiple @Configuration classes listed above.

CodePudding user response:

I would say it is wrong to have single config class due to loosing readablility and mainability as @Bean and @Value methods are growing in numbers. You will not see performance difference as config classes are loaded and scanned during application bootstrap time, so is a one shot operation.

CodePudding user response:

I use a new config class for each package/feature I have. So, I can easily configurate beans for specific Services.

  • Related