Spring的OXM可用于各种情况。 在下面的示例中,我们将使用它将Spring管理的应用程序的设置作为XML文件进行编组。 我们将使用一个简单的JavaBean来表示设置:
@H_404_5@public class Settings {
private boolean fooEnabled;
public boolean isFooEnabled() {
return fooEnabled;
}
void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}
private boolean fooEnabled;
public boolean isFooEnabled() {
return fooEnabled;
}
void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}
应用程序类使用此bean来存储其设置。 除了主要的方法之外,该类还有两种方法:saveSettings()
将设置bean保存到名为settings.xml
的文件中,loadSettings()
再次加载这些设置。 一个main()
方法构造一个Spring应用程序上下文,并调用这两个方法。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
Application {
static final String FILE_NAME = "settings.xml";
private Settings settings = new Settings();
private Marshaller marshaller;
private Unmarshaller unmarshaller;
setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
saveSettings() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
this.marshaller.marshal(settings,new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}
loadSettings() throws IOException {
FileInputStream is = try {
is = new FileInputStream(FILE_NAME);
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
} if (is != null) {
is.close();
}
}
}
static main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveSettings();
application.loadSettings();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
Application {
static final String FILE_NAME = "settings.xml";
private Settings settings = new Settings();
private Marshaller marshaller;
private Unmarshaller unmarshaller;
setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
saveSettings() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
this.marshaller.marshal(settings,new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}
loadSettings() throws IOException {
FileInputStream is = try {
is = new FileInputStream(FILE_NAME);
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
} if (is != null) {
is.close();
}
}
}
static main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveSettings();
application.loadSettings();
}
}
Application
需要设置marshaller
和unmarshaller
属性。 我们可以使用以下applicationContext.xml
:
<beans>
<bean id="application" class="Application">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
</beans>
<bean id="application" class="Application">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
</beans>
该应用程序上下文使用Castor
,但是我们可以使用本章后面描述的其他编组器实例。 请注意,默认情况下,Castor
不需要任何进一步的配置,因此bean定义相当简单。 还要注意的是,CastorMarshaller
实现了Marshaller
和Unmarshaller
,因此我们可以在应用程序的unmarshaller
属性中引用castorMarshaller
bean。
"1.0" encoding="UTF-8" xml version=
<settings foo-enabled="false"/>
<settings foo-enabled="false"/>