Introduction
This is an article where there is a connection with the previous article. The previous article is in this link where the title itself is ‘How to Create a Simple Java Spring Application using NetBeans IDE’. This article will focus on how to pass a Spring Bean to another Spring Bean through its own constructor. It is still using the principle of dependent injection where there will be no need to create or instantiate an object of a Spring Bean in another Spring Bean. The ApplicationContext class will be responsible to perform that task where every Spring Bean will be instantiated upon its own execution.
Passing Spring Bean to another Spring Bean through its own Constructor
Actually, it is a way to implement the principle of dependency injection. The following are the steps for implementing it :
-
First of all, create an interface file. In NetBeans IDE, just right click and select the menu ‘Create Interface’ as in the following display :
How to Pass Spring Bean to another Spring Bean in Java Spring Application using NetBeans IDE -
The following window dialog will appear for asking detail about the interface :
How to Pass Spring Bean to another Spring Bean in Java Spring Application using NetBeans IDE Click the Finish button to execute the process for creating a new interface.
-
Next, edit the interface by adding a new method. Just a simple line to define a new method as follows :
public void task();
Overall, the complete form of the interface after adding one line above exist as follows :
public interface InterfaceEmployeeTask { public void task(); }
-
Continue on, create a new Java class which is going to implement the above interface. For an example, choose the name ‘EmployeeTask’ as follows :
public class EmployeeTask { }
Modify the above source by adding ‘implements InterfaceEmployeeTask’ as in the following source :
public class EmployeeTask implements InterfaceEmployeeTask { }
Finally, since the above source modification of implementing an interface will trigger a warning message in the NetBeans IDE, it will force to implement all of the abstract method. So, the source modification will be in the following form :
public class EmployeeTask implements InterfaceEmployeeTask { @Override public void task() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
In order to present the business logic of the method ‘task’, edit the method. Just give a simple print statement to indicate that the method execution is a success. The following is the modification from the above source code display :
public class EmployeeTask implements InterfaceEmployeeTask { @Override public void task() { System.out.println("My task as an employee is to create a report ..."); // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
-
Following after, do not forget to register the Spring Bean above and pass that Spring Bean reference to the other Spring Bean. Below is the configuration line for registering a new Spring Bean :
<constructor-arg ref="employeeTaskBean" />
Just insert the above line inside the other bean’s definition part :
<bean id="employeeBean" class="com.test.spring.Employee"> <property name="id" value="Employee of the Company" /> <constructor-arg ref="employeeTaskBean" /> </bean>
The above configurations define a Java Spring Bean with the name of ’employeeTaskBean’ will act as an argument for the constructor of the Java Spring Bean with the name of ’employeeBean’.
Overall, the following display is the complete version of the Spring configuration file with the name of ‘spring-config.xml’ as an example :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="employeeBean" class="com.test.spring.Employee"> <property name="id" value="Employee of the Company" /> <constructor-arg ref="employeeTaskBean" /> </bean> <bean id="employeeTaskBean" class="com.test.spring.EmployeeTask"> </bean> </beans>
-
Do not forget to modify the Java Spring Bean with the name of ‘EmployeeBean’ where the representation as a class is in the name of ‘Employee.java’. Add a new constructor by executing a right click in the source code of the Employee.java file. The following image will appear :
How to Pass Spring Bean to another Spring Bean in Java Spring Application using NetBeans IDE -
The following is the complete form after the modification for adding a new constructor with the ‘EmployeeTask’ Java class as its argument. Actually, the ‘EmployeeTask’ Java class represent the ’employeeTaskBean’.
public class Employee { String id; InterfaceEmployeeTask employeeTask; public Employee(InterfaceEmployeeTask employeeTask) { this.employeeTask = employeeTask; } public void doTask(){ employeeTask.task(); } public void setId(String id) { this.id = id; } }
Another important part as in the above line is the addition of a new method with the name ‘doTask()’ for an example. It will call the method task() available in the employeeTask Spring Bean Java Class passed as the constructor. In summary, there is no single line exist which is trying to define the instantiation process or object creation of the employees Task in the above display. That is because of the dependency injection feature in the Spring Framework.
-
Last but not least, execute the Java Spring Application. The following output will appear :
How to Pass Spring Bean to another Spring Bean in Java Spring Application using NetBeans IDE There is a string output ‘My task as an employee is to create a report’ where it is actually the business logic code exist in the task method of the EmployeeTask Java Spring Bean class.