java - Spring - 从 xml 到 Java 配置

标签 java xml spring spring-mvc

我有来自 Spring 项目的 web.xml 和 applicationContext.xml 。 我想更改此设置并仅获取项目的 Java 配置,但我不知道如何更改。

web-xml

<web-app id="WebApp_ID" version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring + JAX-WS</display-name>

  <servlet>
    <servlet-name>jaxws-servlet</servlet-name>
      <servlet-class>
        com.sun.xml.ws.transport.http.servlet.WSSpringServlet
      </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

  <!-- Register Spring Listener -->
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

</web-app>

applicationContext.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:ws="http://jax-ws.dev.java.net/spring/core"
       xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://jax-ws.dev.java.net/spring/core
        http://jax-ws.dev.java.net/spring/core.xsd
        http://jax-ws.dev.java.net/spring/servlet
        http://jax-ws.dev.java.net/spring/servlet.xsd"
>

    <wss:binding url="/hello">
        <wss:service>
            <ws:service bean="#helloWs"/>
        </wss:service>
    </wss:binding>

    <!-- Web service methods -->
    <bean id="helloWs" class="it.capgemini.HelloWorldWS">
      <property name="helloWorldBo" ref="HelloWorldBo" />
    </bean>

    <bean id="HelloWorldBo" class="it.capgemini.soap.HelloWorlBoImpl" />

</beans>

感谢您的建议!

最佳答案

Spring 为 JAX-WS servlet 端点实现提供了一个方便的基类 - SpringBeanAutowiringSupport。为了公开我们的 HelloService,我们扩展 Spring 的 SpringBeanAutowiringSupport 类并在此处实现我们的业务逻辑,通常将调用委托(delegate)给业务层。我们将简单地使用 Spring 的 @Autowired 注释来表达对 Spring 管理的 bean 的此类依赖关系。

@WebService(serviceName="hello")
public class HelloServiceEndpoint extends SpringBeanAutowiringSupport {
    @Autowired
    private HelloService service;

    @WebMethod
    public void helloWs() {
        service.hello();
    }
}

服务本身:

public class HelloService {
    public void hello() {
        // impl
    }
}

以及配置

@Configuration
public class JaxWsConfig {

    @Bean
    public ServletRegistrationBean wsSpringServlet() {
        return new ServletRegistrationBean(new WSSpringServlet(),    "/api/v10");
    }

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }
}

关于java - Spring - 从 xml 到 Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853908/

相关文章:

.net - 如何在自定义 TCP 客户端/服务器之间正确传递 XML 字符串?

java - 两个 docker 容器之间的通信问题

java - Spring MVC JSP Form使用多个对象需要下拉列表

java - Spring boot 嵌入式 tomcat 自定义域

java - 如何使用 OpenGL 将字符串绘制到窗口?

java - java中读取XML的所有父节点

java - 使用 SAX 在节点上运行转换

java - Java 中的 RESTful web 服务,用于下载压缩文件、解压缩并将 PDF 文件显示给客户端

java - 试图从带有 JSON 的 PHP 返回数据到 Android.....

java - 在 Java 中拖动形状