3

Full @Configuration vs “lite” @Bean mode? In Spring documentation they have mentioned something about @Bean annotated method without @Configuration annotated class. Can anyone please help me to get perfect example and where can it useful(in which case)?

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-standard-annotations (Concept : 1.12.1)

  • 2
    Just don't use it, it will lead to more problems than it solves. It basically acts as a factory method (as the documentation states) and for each bean that needs an instance of that bean it will create a fresh one (while most will think it will also produce a singleton). – M. Deinum Jan 04 '22 at 13:16
  • Okay So you mean to say it will act as prototyped bean but originally singletone. Correct? – Maunish Shah Jan 05 '22 at 15:21

1 Answers1

1

When a @Bean method is declared within a @Component annotated class or a plain class, it is not proxied and behaves as a normal java method - so it does not return an instance from the spring container but rather creates a new instance.

This article contains an explanation with examples - https://irena-sayv.medium.com/spring-bean-annotation-what-how-and-why-24f7abe20f83