DEV Community

Santiago Salazar Pavajeau
Santiago Salazar Pavajeau

Posted on

DOM in the java spring framework

Coming into the java spring framework I can't help but associate the concepts from javascript. In this case, it's the document object model (DOM) that has parallels for me.

  • Javascript pairs up with HTML to build the DOM we use on browsers.

  • Java pairs up with XML to also build a DOM in the creation of an app.

However, with javascript, the main use is to manipulate the DOM in order to make changes in the drawing of the browser web page.

But with java, the main use specific to the spring framework is to implement dependency injection and inversion of control.

//left out configration for readability
<beans ...>
     <bean id="store" class="com.santiago.springlab.Store">
        <property name="city" value="Viña"></property>
        <property name="state" value="Valpo"></property>
        <property name="zipCode" value="10000"></property>
    </bean>

    <bean id="prod1" class="com.santiago.springlab.Product" init-method="customInit" destroy-method="customDestroy">
        <property name="pid" value="1"></property>
        <property name="pname" value="cake"></property>
        <property name="store" ref="store"/> <!-- dependency by setter -->
    </bean>
</beans>
Enter fullscreen mode Exit fullscreen mode

Here, this XML associates a Product class with a Store class through the object model, and Product now has access to the Store methods.

However, in the Java class definition, they are not explicitly associated.

//Product.java
package com.santiago.springlab;

public class Product {

    int pid;
    String pname;
    Store store;

    public Product (){
        System.out.println("PRODUCT CONSTRUCTED");
    }

    public Product(Store store) {
        this.store = store; // injection 
    }

    public Product(int pid, String pname, Store store) {
        super();
        this.pid = pid;
        this.pname = pname;
        this.store = store;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Store getStore() {
        return store;
    }

    public void setStore(Store store) {
        System.out.println("setter");
        this.store = store; // setter injection
    }

    public void customInit() {
        System.out.println("Custom initialized");
    }

    public void customDestroy() {
        System.out.println("Custom destroyed");
    }

    @Override
    public String toString() {
        return "Product [pid=" + pid + ", pname=" + pname + ", store=" + store + "]";
    }


}
// Store.java
package com.santiago.springlab;

public class Store {
    String city;
    String state;
    int zipCode;

    public Store() {

    }

    public Store(String city, String state, int zipCode) {
        super();
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getZipCode() {
        return zipCode;
    }

    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return "Store [city=" + city + ", state=" + state + ", zipCode=" + zipCode + "]";
    }


}
Enter fullscreen mode Exit fullscreen mode

So the 'DOM' or context in java can be manipulated with XML beans, just like we modify the 'DOM' with HTML and javascript.

In the end, it is the same tree data structure in both cases, but the uses are different.

Feel more than welcome to reach out with any ideas/comments at Linkedin or Twitter, and check out my portfolio!.

Top comments (0)