Ich beschäftige mich gerad mit XML Marschelling via Spring. Ich hab hier folgende Dateien:
applicationContext.xml
und erhalte die Fehlermeldung:
Da ich ich noch recht unbedarft in Sachen Spring Konfiguration bin, find ich einfach das Problem nicht. Wer kann mir kurz schreiben was ich hier gerad falsch mache.
Code:
package com.javalearning.prototype.spring.xml;
public class TestClass {
private boolean fooEnabled;
public boolean isFooEnabled() {
return fooEnabled;
}
public void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}
Code:
package com.javalearning.prototype.spring.xml;
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;
public class Application {
private static final String FILE_NAME = "settings.xml";
private TestClass testClass = new TestClass();
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void saveTestClass() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
this.marshaller.marshal(testClass, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}
public void loadTestClass() throws IOException {
FileInputStream is = null;
try {
is = new FileInputStream( FILE_NAME );
this.testClass = (TestClass) this.unmarshaller.unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveTestClass();
application.loadTestClass();
}
}
applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="application" class="com.javalearning.prototype.spring.xml.Application">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
</beans>
und erhalte die Fehlermeldung:
HTML:
Exception in thread "main" java.lang.NullPointerException
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:178)
at org.apache.logging.log4j.jcl.LogFactoryImpl$PrivateManager.getContext(LogFactoryImpl.java:109)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:42)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:80)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:154)
at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:89)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.javalearning.prototype.spring.xml.Application.main(Application.java:62)
Da ich ich noch recht unbedarft in Sachen Spring Konfiguration bin, find ich einfach das Problem nicht. Wer kann mir kurz schreiben was ich hier gerad falsch mache.