Spring - 从xml到Java配置

时间:2016-10-04 13:41:52

标签: java xml spring spring-mvc

我有Spring的web.xml和applicationContext.xml项目。 我想改变这个,只为我的项目获得Java配置,但我无法理解。

网络的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>

感谢您的任何建议!

1 个答案:

答案 0 :(得分:2)

Spring为JAX-WS servlet端点实现提供了一个方便的基类 - SpringBeanAutowiringSupport。为了公开我们的HelloService,我们扩展了Spring的SpringBeanAutowiringSupport类并在这里实现了我们的业务逻辑,通常将调用委托给业务层。我们将简单地使用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();
    }
}