XML到Spring Java注释转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML到Spring Java注释转换相关的知识,希望对你有一定的参考价值。

我有一个示例代码,我试图将application-context.xml转换为Spring注释的Java类。如何在这里添加带适当注释的constructor-arg。你能帮我解决这个问题。

应用程序的context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<context:component-scan base-package="myproject"/>

<!-- Define the SOAP version used by the WSDL -->
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
    <property name="soapVersion">
        <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
    </property>
</bean>

<!-- The location of the generated Java files -->
<oxm:jaxb2-marshaller id="marshaller" contextPath="myproject.wsdl.currency"/>

<!-- Configure Spring Web Services -->
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <constructor-arg ref="soapMessageFactory"/>
    <property name="marshaller" ref="marshaller"/>
    <property name="unmarshaller" ref="marshaller"/>
    <property name="defaultUri" value="http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"/>
</bean>
 <bean id="example" class="org.project.Example"/>

我创建了Config.class如下。

@Configuration
public class AppConfig {

@Bean(name="soapMessageFactory")
public SaajSoapMessageFactory getSoapMsgFactory() {
    SaajSoapMessageFactory soapFactory = new SaajSoapMessageFactory();
    soapFactory.setSoapVersion(SoapVersion.11);
    return soapFactory;
}

@Bean(name="webServiceTemplate")
public WebServiceTemplate getWsTemplate(Marshaller marshaller, Unmarshaller unmarshaller) {
    WebServiceTemplate wsTemplate = new WebServiceTemplate();
    //how to configure the constructor-arg here
    wsTemplate.setMarshaller(marshaller);
    wsTemplate.setUnMarshaller(unmarshaller);
    wsTemplate.setDefaultUri(http://www.webservicex.net/CurrencyConvertor.asmx?WSDL);
    return wsTemplate;
}

@Bean(name="example")
public Example getExample() {
   return new ExampleImpl();

}

答案

这应该工作......

@Bean(name="soapMessageFactory")
public SaajSoapMessageFactory getSoapMsgFactory() {
    SaajSoapMessageFactory soapFactory = new SaajSoapMessageFactory();
    soapFactory.setSoapVersion(SoapVersion.11);
    return soapFactory;
}

@Bean(name="webServiceTemplate")
public WebServiceTemplate getWsTemplate(SaajSoapMessageFactory soapFactory, Marshaller marshaller, Unmarshaller unmarshaller) {
    WebServiceTemplate wsTemplate = new WebServiceTemplate(soapFactory);
    wsTemplate.setMarshaller(marshaller);
    wsTemplate.setUnMarshaller(unmarshaller);
    wsTemplate.setDefaultUri(http://www.webservicex.net/CurrencyConvertor.asmx?WSDL);
    return wsTemplate;
}

当您在qazxsw poi方法中指定任何依赖项时,spring将在其上下文中搜索该bean。

由于@Bean是本地创建的,并且没有任何其他依赖关系,因此您可以像这样在本地调用它

SaajSoapMessageFactory

以上是关于XML到Spring Java注释转换的主要内容,如果未能解决你的问题,请参考以下文章

不使用注释的 Java 代码到 XML/XSD

将字符串 XML 片段转换为 Java 中的文档节点

将基于 Java 的配置转换为基于 Spring XML 的配置

java 使用xml和java注释的Spring配置

Spring 注解

spring 注解是有啥作用