How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE

Posted on

Introduction

This is an article where the main content is focusing on how to pass a spring bean to another spring bean as a concept of dependency injection. Actually, this article has a connection with several previous article. The first one is an article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. The connection with that article is because the Java Spring Application in that article become the base for the modification in this article. The second one is an article in this link with the title of ‘How to Pass Spring Bean to another Spring Bean with Constructor in Java Spring Application using NetBeans IDE’. In the second article, the main purpose is actually the same. But in this article, rather using the constructor, it is using a setter method to pass the Spring Bean.

Passing Spring Bean to another Spring Bean through its own Setter Method

So, what is the step for passing the Spring Bean to another Spring Bean as a concept of dependency injection using a setter method ?. The following are the steps for achieving it :

  1. First of all, create an interface. In this example, the interface name is ‘InterfaceEmployeeTraining’. Using NetBeans IDE, just right click at the project and add new interface as in the following display :

    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE
    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE
  2. Edit the interface, add the following method definition so that the complete source code of the interface will exist as follows :

    public interface InterfaceEmployeeTraining {
           public void training();
    }
    
  3. Following after, create the class which is going to implement the interface. For an example, the Java class name is EmployeeTraining. Just edit the file so that it implement the interface class ‘InterfaceEmployeeTraining’ as follows :

    public class EmployeeTraining  {
    
    }
    

    After modification to add the interface implementation, the following is the source code become :

    public class EmployeeTraining implements InterfaceEmployeeTraining {
    
    }
    

    It will force to implement the abstract method available in the interface by giving a warning message. That warning message usually appears in case of using NetBeans IDE or any other IDE to edit the source code. So, the following is another form of the source code after implementing all the abstract method :

    public class EmployeeTraining implements InterfaceEmployeeTraining {
    
          @Override
          public void training() {
                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
          }
    
    }
    
  4. Edit the content of the training method so it will describe a simple business logic by just printing any string information as follows :

    public class EmployeeTraining implements InterfaceEmployeeTraining {
    
          @Override
          public void training() {
                 System.out.println("My first training as an employee is office applications training.");
                 // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
          }
    
    }
    

    Adding a business logic above is important just to prove that the method will be executed in the Java Spring application later on.

  5. After creating the interface and also the class implementing it, just add it into the Java Spring Bean class which acts as a receiver or a injected Spring Bean class. Just do it by defining a setter method in the class. In this case for an example, there is already a Java class file with the name of ‘Employee.java’. If using a NetBeans IDE, just right click at the source code as follows :

    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE
    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE

    Select the Setter… menu and the following window dialog will appear :

    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE
    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE

    Click the checkbox with the label of ’employeeTraining’ to create the setter method for it. After that, just click button Generate to as  in the window dialog above. It will modify the source of the Employee class which exist as follows :

    public class Employee {
           String id;
           InterfaceEmployeeTraining employeeTraining;
           public void setEmployeeTraining(InterfaceEmployeeTraining employeeTraining) {
                  this.employeeTraining = employeeTraining;
           }
    
           public void doTask(){
                  employeeTask.task();
           }
    
           public void doTraining(){
                  employeeTraining.training();
           }
    
           public void setId(String id) {
                  this.id = id;
           }
    
           public void identifyID(){
                  System.out.println("I am an "+ this.id+" !");
           }
    }
  6. Next, register the Java Spring Bean which is passed using a setter method in the Java Spring Configuration file. In this example, it is ‘spring-config.xml’ exist in the ‘src/java/resources’. The following is the configuration line for it :

    <property name="employeeTraining" ref="employeeTrainingBean" />
    

    Insert it into the Java Spring Bean definition which is containing the setter method as follows :

    <bean id="employeeBean" class="com.test.spring.Employee">
    ...
    <property name="employeeTraining" ref="employeeTrainingBean" />
    ...
    </bean>
    
  7. Last but not least, just run the program. The following output will appear :

    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE
    How to Pass a Spring Bean to another Spring Bean with Setter Method in Java Spring Application using NetBeans IDE

    As in the above output of the running program, there is an string printed with the sentence derived from the method training in the EmployeeTraining Java Spring Bean. That is ‘My first training as an employee is office applications training.’.

Leave a Reply