我有来自 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/