将基于XML的Spring转换为基于Java的配置 [英] Convert Spring XML-based to Java-Based Configuration
本文介绍了将基于XML的Spring转换为基于Java的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尽量不要使用任何xml。
I try not to using any xml.
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller"/>
<property name="unmarshaller" ref="jaxbMarshaller"/>
</bean>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
</list>
</property>
</bean>
像这样:convert to @Bean
like this one: convert to @Bean
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(marshallingMessageConverter());
restTemplate.setMessageConverters(converters);
return restTemplate;
}
这里有问题。
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cloudlb.domain.User</value>
</list>
</property>
</bean>
尝试将com.cloudlb.domain.User转换为Class [] not thing work。 / p>
Try to convert "com.cloudlb.domain.User" into Class [] not thing work.
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
//
List<Class<?>> listClass = new ArrayList<Class<?>>();
listClass.add(User.class);
marshaller.setClassesToBeBound((Class<?>[])listClass.toArray());
// --------------------------------
return new MarshallingHttpMessageConverter(marshaller, marshaller);
}
错误:铸造有问题。
提前谢谢。
推荐答案
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
return new MarshallingHttpMessageConverter(
jaxb2Marshaller(),
jaxb2Marshaller()
);
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{
twitter.model.Statuses.class
});
return marshaller;
}
这篇关于将基于XML的Spring转换为基于Java的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文