2016-10-04 130 views
1

我有来自Spring项目的web.xml和applicationContext.xml。 我想改变这一点,只获得我的项目的Java配置,但我不知道如何。Spring - 从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> 

感谢您的建议!

+0

的可能的复制[Spring配置文件(http://stackoverflow.com/questions/6283653/spring-configuration) – Dez

+0

希望在http://stackoverflow.com/questions/39851336/spring-utilizing-mongo-implementation-over-jpa/39852442#39852442帮助编辑。 – HARDI

回答

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(); 
    } 
} 
+0

感谢您的评论。 所以我可以使用这两个类而不是xml配置文件? – bigghe

+0

是的,你也需要添加配置到你的应用程序。 –

+0

我无法找到WSSpringServlet(),我在SpringBoot 1.3.3 – bigghe