Google News
logo
Java Springs - Interview Questions
How does Spring dependency injection work behind the scenes
When you define a Bean to be injected in another class, Spring creates an object of the Bean which is managed by the Spring container ready for use.
 
If you decide to use constructor injection, create another Bean and reference to the previous object to be injected in this class.
 
For example, we can create an email service Bean, which will be injected into the customer to provide messaging services, as shown below.
<bean id="emailService"
   class="com.freetimelearn.app.EmailService">
</bean>
 
<bean id="customer"
   class="com.freetimelearn.app.Customer">
   <constructor-arg ref="emailService">
</bean>
 The above configuration file will be translated to the following by Spring.
EmailService theEmailService = new EmailService();
 
Customer theCustomer = new Customer(theEmailService);
Advertisement