简体   繁体   English

Spring - 从 xml 到 Java 配置

[英]Spring - From xml to Java config

I have web.xml and applicationContext.xml from Spring's project.我有来自 Spring 项目的 web.xml 和 applicationContext.xml。 I want to change this and get only Java configuration for my project but I can't figure how.我想改变这一点,只为我的项目获取 Java 配置,但我不知道如何。

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

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

Thanks for any suggestion!感谢您的任何建议!

Spring provides a convenient base class for JAX-WS servlet endpoint implementations - SpringBeanAutowiringSupport . Spring 为 JAX-WS servlet 端点实现提供了一个方便的基类 - SpringBeanAutowiringSupport To expose our HelloService we extend Spring's SpringBeanAutowiringSupport class and implement our business logic here, usually delegating the call to the business layer.为了公开我们的HelloService我们扩展 Spring 的SpringBeanAutowiringSupport类并在这里实现我们的业务逻辑,通常将调用委托给业务层。 We'll simply use Spring's @Autowired annotation for expressing such dependencies on Spring-managed beans.我们将简单地使用 Spring 的@Autowired注释来表达对 Spring 管理的 bean 的这种依赖关系。

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

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

The service itself:服务本身:

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

And configuration和配置

@Configuration
public class JaxWsConfig {

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM