Step 7: Running the main class and comparing the results with different scopes. The for loop was created to instantiate more than one bean. Look at the memory address of the beans printed to see how many instances are created.

You can only see 1 instance of service bean and 1 instance of prototype bean. This is not desired as in some scenarios you want to create new prototype bean again and again. This is where lookup-method comes in handy.

2 Comments:

In the above example when the DAO is made prototype but the service is singleton why only one instance of the DAO bean is created but on the other hand when you make the service as prototype multiple instances of services are created on running the main.

DAO gets injected into Service. There is only a single instance of service, which takes a single instance of DAO when initialized. When you have Service as prototype, each thread will have its own instance, and same DAO gets injected into each new instance of service.