DEV Community

Santiago Salazar Pavajeau
Santiago Salazar Pavajeau

Posted on

2 2

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!.

Quadratic: The AI-Powered Spreadsheet for Modern Teams cover image

Quadratic: The AI-Powered Spreadsheet for Modern Teams

Quadratic modernizes spreadsheets with AI integration, real-time collaboration, and support for SQL, Python, and JavaScript, offering a powerful, accessible data analysis tool without setup.

Read full post

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay