Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spring dependency injection create singleton objects?

How does spring manage these objects internally?

When are the singleton objects created and destroyed?

like image 463
Parag Avatar asked Oct 26 '25 11:10

Parag


1 Answers

By default, Spring beans are basically singletons. Spring will create a single instance of each of your classes, and it will generally do this when Spring is initialized.

However, this is nearly infinitely customizable. Spring can also create a new instance of a bean whenever you ask for that bean by name, or it can create beans for the length of a request, or it can use a singleton but not instantiate it until the first time you invoke it, or several other things.

The discussion of when beans are instantiated and how long they last is a discussion of a bean's "Scope". Check it out in the documentation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes

like image 162
Brandon Yarbrough Avatar answered Oct 29 '25 01:10

Brandon Yarbrough



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!