mstrom
mstrom

Reputation: 1743

Spring - what's a bean and what's not?

I'm new to Spring and I'm confused about something basic. Are the classes that are stereotyped (Service, Controller, Repository) treated as beans? I'm confused as to when you actually need to annotate/configure something as a bean and when you don't. Is it for the classes that aren't stereotyped?

Thanks!

Upvotes: 1

Views: 65

Answers (1)

gipinani
gipinani

Reputation: 14398

From spring documentation:

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Service, Controller, Repository are managed by the Spring IoC container, so they are called beans. You annotate a class as @Serivice, @Controller, @Repository, or more in general @Component when you want spring to manage it: spring will manage the instance of annotated class in regard of the scope you select (not all these scope are always available):

  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request
  4. session – Return a single bean instance per HTTP session
  5. globalSession – Return a single bean instance per global HTTP session

Upvotes: 1

Related Questions