如何将Spring Config XML迁移到Spring Boot注释

时间:2015-10-02 13:00:20

标签: java spring spring-boot spring-annotations

我正在使用Spring启动来创建XML编组的应用程序。我按照一些教程来启动应用程序。我想避免使用xml绑定文件的任何Spring配置,而是想使用注释。

我不确定如何将此配置文件迁移到注释驱动的应用程序中。

 <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>

目前我已经写了这段代码:

@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);
    }
}

而且:

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();
            }
        }
    }
}

我在getMarshaller().marshal(object, new StreamResult(fileOutputStream));得到一个Nullpointer,因为marshaller为null。

在配置文件中,有对CastorMarshaller类和marshaller属性的引用。

如何将其迁移到注释驱动的应用程序?

感谢您的帮助。

EDIT1:

<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
    <property name="mappingLocation" value="classpath:mapping.xml" />
</bean>

3 个答案:

答案 0 :(得分:2)

以这种方式改变第一主要课程:

@SpringBootApplication
public class Application{
    public static void main(String[] args) {
        ConfigurableApplicationContext context =
            SpringApplication.run(Application.class, args);

        XMLConverter converter = context.getBean(XMLConverter.class)
        Whitelist whitelist = new Whitelist("example");
        converter.convertFromObjectToXML(whitelist, XML_FILE_NAME);
    }

    @Bean
    public XMLConverter xmlConverter(){
        XMLConverter converter = new XMLConverter();
        CastorMarshaller castorMarshaller = new CastorMarshaller()
        converter.setMarshaller(castorMarshaller);
        converter.setUnmarshaller(castorMarshaller);
        return converter;
    }
}

怎么一回事,因为:

  1. @SpringBootApplication使用自动配置+组件扫描
  2. 您不应该使用@Component
  3. 注释@Configuraition类
  4. 您不使用命令行参数
  5. 不了解尝试创建自定义转换器

答案 1 :(得分:1)

首先:@Configuration注释应该用于定义应用程序上下文的Java配置的类,该应用程序上下文通常是一个单独的文件。而是使用@Component注释您的课程:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
@Component
public class Application implements CommandLineRunner{
...

此外,在XML转换器类中,必须对marshaller进行注释,因此可以使用bean定义进行连接: @Configuration

public class XMLConverter {

    @Autowired
    private Marshaller marshaller;
...

最后你需要一个应用程序上下文类:

@Configuration
public class ApplicationContext {
    @Bean
    public CastorMarshaller castorMarshaller() {
        return new CastorMarshaller();
    }

    @Bean
    public XMLConverter XMLConverter() {
        XMLConverter convertor = new XMLConverter();
        CastorMarshaller marshaller = castorMarshaller();
        convertor.setMarshaller(marshaller);
        convertor.setUnmarshaller(marshaller);
    }
}

将应用程序main方法中的代码更改为:

SpringApplication.run(ApplicationContext .class, args);

编辑:带有mappingLocation的CastorMarhsaller

   @Bean
    public CastorMarshaller castorMarshaller() {
        ClassPathContextResource resource = new ClassPathContextResource("mapping.xml", getClass().getClassLoader());
        CastorMarshaller marshaller = new CastorMarshaller();
        marshaller.setMappingLocation(resource);
        return marshaller;
    }

如果在其他地方也使用相同的资源,您可以将其定义为bean,并以与上面所示类似的方式重复使用它。

答案 2 :(得分:0)

您应将@Autowired放在private Marshaller marshaller;XMLConverter上:  @Autowired private Marshaller marshaller;