📜  带脚轮的弹簧示例

📅  最后修改于: 2022-05-13 01:54:56.552000             🧑  作者: Mango

带脚轮的弹簧示例

通过使用 CastorMarshaller 类,我们可以在使用 castor 的帮助下实现将Java对象编组为 XML 代码,反之亦然。 castor 是其中 Marshaller 和 Unmarshaller 接口的实现类,因此默认情况下不需要其他进一步的配置。 Spring 和 Castor 集成最常见的示例是“将Java对象编组到 XML 中。

实现:我们需要创建以下文件,以便在使用 Castor 的 Spring 的 XML 中编组Java对象:

  1. 工人。Java
  2. aplcontext.xml
  3. 映射器.xml
  4. 用户。Java

Jar文件的要求如下:

A. 档案:工人。Java

这个文件用setter和getter定义了worker的id、name和salary这三个属性。

Java
// Java Program to Illustrate Worker Class
 
package com.geeksforgeeks;
 
// Class
public class Worker {
 
    // Class data members
 
    // Worker id
    private int id;
    // Worker name
    private String name;
    // Worker Salary
    private float sal;
 
    // Getter
    public int gettingId()
    {
        return id;
    }
 
    // Setter
    public void settingId(int id)
    {
        this.id = id;
    }
 
    // Getter
    public String gettingName()
    {
        return name;
    }
 
    // Setter
    public void settingName(String name)
    {
        this.name = name;
    }
 
    // Getter
    public float gettingSal()
    {
        return salary;
    }
 
    // Setter
    public void settingSal(float sal)
    {
        this.sal = sal;
    }
}


XML
 
 
   
 
        
   
        
   
     


XML
 
 
  
     
         
       
       
            
           
         
       
         
            
           
           
         
         
            
           
         
       
     
 


Java
// Java Program to Illustrate Application Class
 
package com.geeksforgeeks;
 
// Importing required classes
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;
 
// Main Class
public class User {
 
    // Main driver method
    public static void main(String[] args) throws IOException
    {
 
        // Creating object of ApplicationContext and
        // Marshaller class
        ApplicationContext context = new ClassPathXmlApplicationContext("aplcontext.xml");
        Marshaller marshaller = (Marshaller)context.getBean("castorMarshallerBean");
 
        // Creating object of Worker class
        Worker worker = new Worker();
        worker.settingId(123);
        worker.settingName("Rushikesh Khurpade");
        worker.settingSal(300000);
 
        marshaller.marshal(worker, new StreamResult(new FileWriter("worker.xml")));
 
        // Display message for successful execution
        // of program
        System.out.println(" XML Created Successfully ");
    }
}


XML
 
 
Rushikesh Khurpade 
300000.0 


B. 文件:aplcontext.xml

这个文件定义了一个 bean castorMarshallerBean,因为 'Worker class' 与 OXM 框架绑定

XML

 
 
   
 
        
   
        
   
     

C. 文件:mapper.xml

XML

 
 
  
     
         
       
       
            
           
         
       
         
            
           
           
         
         
            
           
         
       
     
 

D. 文件:用户。Java

该文件从 aplcontext.xml 文件中获取 Marshaller 的实例,并通过它调用 marshal 方法

Java

// Java Program to Illustrate Application Class
 
package com.geeksforgeeks;
 
// Importing required classes
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;
 
// Main Class
public class User {
 
    // Main driver method
    public static void main(String[] args) throws IOException
    {
 
        // Creating object of ApplicationContext and
        // Marshaller class
        ApplicationContext context = new ClassPathXmlApplicationContext("aplcontext.xml");
        Marshaller marshaller = (Marshaller)context.getBean("castorMarshallerBean");
 
        // Creating object of Worker class
        Worker worker = new Worker();
        worker.settingId(123);
        worker.settingName("Rushikesh Khurpade");
        worker.settingSal(300000);
 
        marshaller.marshal(worker, new StreamResult(new FileWriter("worker.xml")));
 
        // Display message for successful execution
        // of program
        System.out.println(" XML Created Successfully ");
    }
}

输出:

XML Created Successfully 

文件:worker.xml

XML

 
 
Rushikesh Khurpade 
300000.0