Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring XML-Based DI and Builder Pattern

DZone's Guide to

Spring XML-Based DI and Builder Pattern

This quick tutorial covers creating Spring beans for classes created with the Builder pattern using XML.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Recently, I faced a situation where a project was using Spring for its business layer and maintains the Spring dependencies in an XML file. They cannot upgrade this XML-based configuration to a Java-based configuration. It's an old project, it will take time, and they can't afford it.

That's fine, but the problem is that they want to add a third-party module to re-use some utilities from that library. When they try to incorporate the aforesaid module, they discovered that many classes that are exposed as services were created using the Builder pattern. So we will see how we can incorporate the Builder pattern in Spring XML-based DI.

Builder Pattern

The Builder pattern is used to create complex objects — which have some required and some optional parameters.

We create a static inner class called Builder and pass the required parameters in the constructor of that Builder. Then, set optional parameters calling the Setter method of that Builder.

Lastly, it has a build() method, which will produce the actual object.

Say we create an Employee object using the Builder pattern, where the id and the name of the employee are mandatory, but hobbies, gender, and address are optional.

package com.example.advanceSpring.entity;

public class Employee {   
    private Long id;
    private String name;
    private int gender;
    private String address;
    private String hobby;

    private Employee() {}

    public static class EmployeeBuilder{
        private Long id;
        private String name;
        private int gender;
        private String address;
        private String hobby;

        public EmployeeBuilder(Long id, String name) {
            this.id=id;
            this.name=name;
        }       

        public void setGender(int gender) {
            this.gender = gender;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public void setHobby(String  hobby) {
            this.hobby = hobby;
        }

        public Employee build() {
            Employee emp = new Employee();
            emp.id=id;
            emp.name=name;
            emp.gender=gender;
            emp.address=address;
            emp.hobby=hobby;
            return emp;
        }
    }

    @Override
    public String toString() {       
        return "Employee [id=" + id + ", name=" + name + ", gender="+( gender == 1?"Male":"Female") + ", address=" + address + ", hobby="
                + hobby + "]";
    }
}

Creating the Bean

Let's say this class comes from a third party JAR and we need to create an Employee object using Spring XML-based dependency injection.

How can we do that?

  1. Register the builder class using the context of Employee class with $. So the class attribute will be class =”com.example.advanceSpring.entity.Employee$EmployeeBuilder”. By doing this, Spring will understand that Employee builder is an inner class of Employee.

  2. Pass the required parameters using constructor-args.

  3. Pass the optional parameter using the property tag.

  4. Register the Employee bean in Spring XML. While registering, one thing you can notice is that the Employee object constructor is private, so the Spring bean can’t call it by reflection. So we will use the factory-bean and the factory-method attribute of Spring XML.

    The factory-bean attribute tells Spring to use the bean as a Factory class for creating the Object, so here, Spring treats EmployeeBuider as a Factory of Employee Object.

    The factory-method attribute tells Spring, upon calling the method instance of the Object, to return, so here, the build() method acts as a factory method to create the Employee Object.

    <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-2.5.xsd">
    
        <bean id="helloBean" class="com.example.advanceSpring.HelloWorld">
            <property name="name" value="Shamik" />
        </bean>
    
        <bean id="builder" class="com.example.advanceSpring.entity.Employee$EmployeeBuilder">
            <constructor-arg value="1"/>
           <constructor-arg value="Shamik Mitra"/>
           <property name="gender" value="1"/>
           <property name="hobby" value="Blogging"/>
        </bean>
        <bean id="employee" class="com.example.advanceSpring.entity.Employee" factory-bean="builder" factory-method="build"/>
    
    </beans>
    
  5. Now we will test the settings.

    import org.springframework.context.ApplicationContext;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.example.advanceSpring.entity.Employee;
    
    public class SpringTest {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "SpringTest.xml");
    
            Employee employee = (Employee) context.getBean("employee");
            System.out.println(employee);
        }
    
    }
  6. Output:

    Employee [id=1, name=Shamik Mitra, gender=Male, address=null, hobby=Blogging]

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
xml ,builder pattern ,spring bean ,java ,tutorial ,dependency injection

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}