vendredi 20 mars 2015

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?


Aucun commentaire:

Enregistrer un commentaire