Is Spring Dependency Injection only for singleton instances?

Sub Vengeance :

I am currently studying about the spring framework and its dependency injection system.

I have learned about the use of FileSystemXmlApplicationContext and how we define the dependencies in a XML file using the Beans tag.

I understand that the beans are so called Spring Beans which are singleton types by nature. With this in mind that begs the question if the Spring DI system is only for singleton classes?

Aditya Rewari :

There are various other scopes offered by Spring DI, other than Singleton

Listing various Spring scopes:

  • singleton
  • prototype
  • request
  • session
  • application
  • websocket

Prototype : This scope will give you a new object on every DI.

Source: https://www.baeldung.com/spring-bean-scopes

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334670&siteId=1