OXM in Spring

OXM in Spring:

Spring and JAXB Integration

JAXB is an acronym for Java Architecture for XML Binding. It enables java engineers to delineate class to XML portrayal. JAXB can be utilized to marshal java objects into XML and the other way around.It is an OXM (Object XML Mapping) or O/M structure gave by Sun.

Preferred standpoint of JAXB

No compelling reason to make or utilize a SAX or DOM parser and compose callback strategies.

  1. Employee.java
  2. applicationContext.xml
  3. Client.java

Required Jar files

  • Spring Core jar files
  • Spring Web jar files

JAVA Tutorial Video

 Employee.java

  1. @XmlRootElement It specifies the root element for the xml file.
  2. @XmlAttribute It specifies attribute for the property.
  3. @XmlElement It specifies the element.

 package com.javaspot;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name=”employee”)

public class Employee {

private int id;

private String name;

private float salary;

@XmlAttribute(name=”id”)

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@XmlElement(name=”name”)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@XmlElement(name=”salary”)

public float getSalary() {

return salary;

}

public void setSalary(float salary) {

this.salary = salary;

}

}

applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:oxm=”http://www.springframework.org/schema/oxm”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/oxm

http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd”>

<oxm:jaxb2-marshaller id=”jaxbMarshallerBean”>

<oxm:class-to-be-bound name=”com.javaspot.Employee”/>

</oxm:jaxb2-marshaller>

</beans>

Client.java

package com.javaspot;

import java.io.FileWriter;

import java.io.IOException;

import javax.xml.transform.stream.StreamResult;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.oxm.Marshaller;

public class Client{

public static void main(String[] args)throws IOException{

ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);

Marshaller marshaller = (Marshaller)context.getBean(“jaxbMarshallerBean”);

Employee employee=new Employee();

employee.setId(101);

employee.setName(“Sonoo Madhu”);

employee.setSalary(100000);

marshaller.marshal(employee, new StreamResult(new FileWriter(“employee.xml”)));

System.out.println(“XML Created Sucessfully”);

}

}

Output
employee.xml

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>

<employee id=”101″>

<name>Sonoo Madhu</name>

<salary>100000.0</salary>

</employee>

Spring with Xstream 

Xstream is a library to serialize articles to xml and the other way around without necessity of any mapping document. Notice that castor requires a mapping record.

XStreamMarshaller class gives office to marshal objects into xml and the other way around.

Spring with Castor:

By the assistance of Castor Marshaller class, we can marshal the java objects into xml and the other way around utilizing castor. It is the usage class for Marshaller and Unmarshaller interfaces. It doesn’t require any further arrangement by default.

Spring and Castor Integration (Marshalling Java Object into XML)
  1. Employee.java
  2. applicationContext.xml
  3. mapping.xml
  4. Client.java
Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files
  • castor-1.3.jar
  • castor-1.3-core.jar

Spring and Struts 2 Integration

Spring system gives a simple approach to deal with the reliance. It can be effectively coordinated with struts 2 system.

The ContextLoaderListener class is utilized to impart spring application with struts 2. It must be indicated in the web.xml record.

 You have to take after strides:

  • Make struts2 application and include spring container records.
  • In web.xml record, characterize ContextLoaderListener class.
  • In struts.xml record, characterize bean name for the activity class.
  • In the activity class, characterize additional property e.g. message.
Spring and Struts 2 Integration
  1. index.jsp
  2. web.xml
  3. struts.xml
  4. applicationContext.xml
  5. Login.java
  6. welcome.jsp
  7. error.jsp

 

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.