Shiga S
Shiga S

Reputation: 11

Does Spring DI create multiple instances while injecting through @Autowired?

I have a class A and class B which are using methods from class C.

Class C is a service class annotated with @Service.

To get object instances of class C in class A and B, I have used @Autowired.

This means that Spring will create two object instances, one for class A and one for class B ?

Upvotes: 1

Views: 523

Answers (1)

Anish B.
Anish B.

Reputation: 16529

No, Spring creates singleton (default scope) bean in the IOC container.

So, only one bean will be created for class C.

That particular bean or object of class C will be shared for all the classes whether it's class A or B or whatever.

Upvotes: 2

Related Questions