Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put @Bean in Spring Boot?

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:

  1. 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);     } } 
  2. 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?

like image 774
Marco Avatar asked Mar 19 '15 13:03

Marco


People also ask

Where can I use @bean annotation?

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.

Is @bean used in Spring boot?

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.

When should you use @bean Spring?

@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.


2 Answers

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.

like image 98
Daniel Cottone Avatar answered Oct 05 '22 13:10

Daniel Cottone


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.

like image 25
Naresh Joshi Avatar answered Oct 05 '22 15:10

Naresh Joshi