I am wondering what the best place would be for a Spring Boot app to register additional beans. I have a Main class that is annotated with @SpringBootApplication
and beans defined in that class are picked up. But when i put those beans in another class it seems that the are not being registered.
When reading the documentation i got the idea that the @SpringBootApplication
would implicitly search for classes that have @Bean
annotations in them.
So my options are now:
Put all @Bean
annotated bean in my main class
@SpringBootApplication public class MyApplication { @Bean public Filter AuthenticationFilter() { return new AuthenticationFilter(); } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
Create a configuration class and annotate that with @Configuration
@Configuration public class MyConfiguration { @Bean public Filter AuthenticationFilter() { return new AuthenticationFilter(); } }
Is there a better way of doing this?
One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.
In Spring Boot @Bean tutorial, we create a simple Bean in Spring Boot framework using the @Bean annotation. Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications with minimal effort.
@Bean is used to mark a method as one that creates a bean and Spring will then add it to the context for us. The return type of the method defines the type of bean that is created, so both of the beans created in this example will be referred to by the type MyBean rather than their implementations.
It is pretty much a matter of preference, but it is generally considered best practice to put exposed beans in configuration classes, which are logically grouped.
For example, you might have several configuration classes with a number of beans contained within each: An authentication configuration class with beans for AuthenticationProvider or UserDetailsService; a Thymeleaf configuration class containing beans for various Thymeleaf dialects, etc.
Actually, it is your choice there is no spring standard present to tell which one is best but while defining a class OOP design principles says A class should be loosely coupled
and highly cohesive
, should follow Single Responsibility Principle (SRP)
, Here
Coupling
--> Degree of knowledge a class has about another class
Cohesion
--> Degree which tells how well focused your class is
SRP
--> A class should have only one responsibility, there should be only one reason to change a class.
So according to cohesion and SRP principle class should be well focused and have only one responsibility.
Here in your case you have only 2 beans but in future, these beans might increase. So should follow your second point and create another class for your bean declaration.
And In my choice should even create more configuration classes, So one configuration class should have a similar type of beans.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With