- Spring和脚轮整合
- Spring和脚轮整合的例子
Spring和Castor集成的示例(将Java对象编组为XML)
你需要创建以下文件, 以使用带有Castor的Spring将Java对象编组为XML:
- Employee.java
- applicationContext.xml
- mapping.xml
- Client.java
- Spring Core jar文件
- Spring Web jar文件
- castor-1.3.jar
- castor-1.3-core.jar
下载castor-1.3.jar
下载castor-1.3-core.jar
Employee.java
如果定义了三个属性id, setter和getter的名称和薪水。
package com.srcmini;
public class Employee {
private int id;
private String name;
private float salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
applicationContext.xml
它定义了一个bean castorMarshallerBean, 其中Employee类与OXM框架绑定在一起。
<
?xml version="1.0" encoding="UTF-8"?>
<
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-3.0.xsd">
<
bean id="castorMarshallerBean" class="org.springframework.oxm.castor.CastorMarshaller">
<
property name="targetClass" value="http://www.srcmini.com/com.srcmini.Employee">
<
/property>
<
property name="mappingLocation" value="http://www.srcmini.com/mapping.xml">
<
/property>
<
/bean>
<
/beans>
mapping.xml
<
?xml version="1.0"?>
<
!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<
mapping>
<
class name="com.srcmini.Employee" auto-complete="true" >
<
map-to xml="Employee" ns-uri="http://www.srcmini.com" ns-prefix="dp"/>
<
field name="id" type="integer">
<
bind-xml name="id" node="attribute">
<
/bind-xml>
<
/field>
<
field name="name">
<
bind-xml name="name">
<
/bind-xml>
<
/field>
<
field name="salary">
<
bind-xml name="salary" type="float">
<
/bind-xml>
<
/field>
<
/class>
<
/mapping>
Client.java
它从applicationContext.xml文件获取Marshaller的实例, 并调用marshal方法。
package com.srcmini;
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("castorMarshallerBean");
Employee employee=new Employee();
employee.setId(101);
employee.setName("Sonoo Jaiswal");
employee.setSalary(100000);
marshaller.marshal(employee, new StreamResult(new FileWriter("employee.xml")));
System.out.println("XML Created Sucessfully");
}
}
示例输出
employee.xml
<
?xml version="1.0" encoding="UTF-8"?>
<
dp:Employee xmlns:dp="http://www.srcmini.com" id="101">
<
dp:name>
Sonoo Jaiswal<
/dp:name>
<
dp:salary>
100000.0<
/dp:salary>
<
/dp:Employee>
下载此示例(使用Myeclipse IDE开发)
【Spring Castor用法示例详解】下载此示例(使用Eclipse IDE开发)
推荐阅读
- Spring和Struts 2集成详细步骤和示例
- Spring通过HTTP Invoker进行远程处理示例
- Spring和Struts 2集成的登录详细示例
- Android精通(View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件)
- android greenDao使用
- Android 应用资源
- Android 9 Pie震撼来袭 同步登陆WeTest
- appium常用方法整理
- hadoop 3.1.1 Could not find or load main class org.apache.hadoop.mapreduce.v2.app.MRAppMaster