Gewusst wie: migrieren von Spring Config XML für Spring-Boot-Anmerkungen

Bin ich mit Spring boot zu erstellen, eine Anwendung für XML-marshalling. Ich folgte einige Anleitungen, um die Anwendung zu starten. Ich möchte vermeiden, jedes Frühjahr config mithilfe von xml-binding-Datei und möchte die Anmerkungen statt.

Ich bin nicht sicher, wie migrieren Sie die Konfigurationsdatei in annotation-driven Anwendung.

 <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="XMLConverter" class="com.java2s.common.XMLConverter">
    <property name="marshaller" ref="castorMarshaller" />
    <property name="unmarshaller" ref="castorMarshaller" />
  </bean>
  <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
</beans>

Im moment Schreibe ich diesen code:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@Configuration
public class Application implements CommandLineRunner{

    private static final String XML_FILE_NAME = "whitelist.xml";

    @Autowired
    XMLConverter converter;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        Whitelist whitelist = new Whitelist("example");
        converter.convertFromObjectToXML(whitelist, XML_FILE_NAME);
    }
}

Und so:

import javax.xml.transform.stream.StreamResult;

import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.Marshaller;

@Configuration
public class XMLConverter {


    private Marshaller marshaller;

    public Marshaller getMarshaller() {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    }

    public void convertFromObjectToXML(Object object, String filepath) throws IOException{

        FileOutputStream fileOutputStream = null;
        try{
            fileOutputStream = new FileOutputStream(filepath);
            getMarshaller().marshal(object, new StreamResult(fileOutputStream));
        }finally {
            if(fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }
}

Bekomme ich eine Nullpointer bei getMarshaller().marshal(object, new StreamResult(fileOutputStream)); weil marshaller null ist.

In der config-Datei es ist der Verweis auf die CastorMarshaller Klasse und die marshaller-Eigenschaft.

Wie kann ich migrieren diese in eine annotation-driven Anwendung?

Vielen Dank für Ihre Hilfe.

EDIT1:

<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
    <property name="mappingLocation" value="classpath:mapping.xml" />
</bean>
InformationsquelleAutor Patrick | 2015-10-02
Schreibe einen Kommentar