Where to put @Bean in Spring Boot?

前端 未结 6 418
清歌不尽
清歌不尽 2020-12-24 05:32

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

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 05:47

    Yes, including your beans inside the @Configuration class is usually the preferred way for Spring.

    This is also one of the ways Spring recommends injecting inter-dependencies between beans is shown in the following sample copied from the Spring's reference guide here:

    Additionally, the default scope of @Beans is SINGLETON, if you specify a different scope such as PROTOTYPE the call will be passed to the original method. Have a look at this section in the Spring Reference Guide

提交回复
热议问题