在我看来,即使我使用 @Configuration 类来连接所有的 bean,Spring 的 Web 服务部分仍然需要在 XML 文件中进行一些配置。有谁知道如何在没有任何 XML 文件的情况下设置 Spring Web 服务?
在这一点上,它是 JAXWS 还是 SpringWS 并不重要,只要它可以正常工作。
我将 Spring 3.1.2 与 JAX-WS 2.2.3 一起使用。这是我到目前为止所拥有的:
//The endpoint
@WebService(serviceName = "EquityService")
public class EquityServiceEndPoint extends SpringBeanAutowiringSupport implements EquityService {
@Autowired
private EquityService equityService;
@WebMethod
public String helloWorld() {
return equityService.helloWorld();
}
}
这是配置类:
@Configuration
public class MarketServerConfiguration {
@Bean
public EquityService equityService() {
return new EquityServiceImpl();
}
@Bean
public EquityServiceEndPoint equityServiceEndPoint() {
return new EquityServiceEndPoint();
}
}
这是 web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
version="2.4">
<display-name>MarketService</display-name>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.hello.market.server.MarketServerConfiguration</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>/service/*</url-pattern>
</servlet-mapping>
</web-app>
我用 Jetty 运行整个过程。当我转到 http://localhost:8888/service 时,我收到一条消息“404 Not Found: Invalid Request”,在日志中我可以看到 JAX-WS servlet 已初始化,因为它打印
Jan 26, 2013 4:05:38 PM com.sun.xml.ws.transport.http.servlet.WSServletDelegate <init>
INFO: WSSERVLET14: JAX-WS servlet initializing
但是,如果我访问 http://localhost:8888/service/EquityService,我会收到相同的“404 Not Found: Invalid request”消息,并且在日志中我可以看到没有发生任何事情。
最后,这是整个日志。据我所知,一切都很好,所有的豆子都连接好了……
>>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP
2013-01-26 16:05:32,993 INFO org.mortbay.log.info:67 - jetty-6.1.25
2013-01-26 16:05:33,332 INFO /.info:67 - Initializing Spring root WebApplicationContext
2013-01-26 16:05:33,332 INFO org.springframework.web.context.ContextLoader.initWebApplicationContext:272 - Root WebApplicationContext: initialization started
2013-01-26 16:05:33,432 DEBUG org.springframework.web.context.support.StandardServletEnvironment.<init>:114 - Initializing new StandardServletEnvironment
2013-01-26 16:05:33,433 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2013-01-26 16:05:33,434 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [servletContextInitParams] PropertySource with lowest search precedence
2013-01-26 16:05:33,443 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [jndiProperties] PropertySource with lowest search precedence
2013-01-26 16:05:33,444 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [systemProperties] PropertySource with lowest search precedence
2013-01-26 16:05:33,446 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [systemEnvironment] PropertySource with lowest search precedence
2013-01-26 16:05:33,446 DEBUG org.springframework.web.context.support.StandardServletEnvironment.<init>:120 - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2013-01-26 16:05:33,450 INFO org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh:500 - Refreshing Root WebApplicationContext: startup date [Sat Jan 26 16:05:33 CET 2013]; root of context hierarchy
2013-01-26 16:05:33,457 DEBUG org.springframework.web.context.support.StandardServletEnvironment.replace:161 - Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2013-01-26 16:05:33,509 DEBUG org.springframework.core.env.StandardEnvironment.<init>:114 - Initializing new StandardEnvironment
2013-01-26 16:05:33,510 DEBUG org.springframework.core.env.StandardEnvironment.addLast:104 - Adding [systemProperties] PropertySource with lowest search precedence
2013-01-26 16:05:33,510 DEBUG org.springframework.core.env.StandardEnvironment.addLast:104 - Adding [systemEnvironment] PropertySource with lowest search precedence
2013-01-26 16:05:33,511 DEBUG org.springframework.core.env.StandardEnvironment.<init>:120 - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2013-01-26 16:05:33,540 DEBUG org.springframework.core.env.StandardEnvironment.<init>:114 - Initializing new StandardEnvironment
2013-01-26 16:05:33,541 DEBUG org.springframework.core.env.StandardEnvironment.addLast:104 - Adding [systemProperties] PropertySource with lowest search precedence
2013-01-26 16:05:33,542 DEBUG org.springframework.core.env.StandardEnvironment.addLast:104 - Adding [systemEnvironment] PropertySource with lowest search precedence
2013-01-26 16:05:33,542 DEBUG org.springframework.core.env.StandardEnvironment.<init>:120 - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2013-01-26 16:05:33,564 INFO org.springframework.context.annotation.ClassPathBeanDefinitionScanner.registerDefaultFilters:202 - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2013-01-26 16:05:33,568 INFO org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions:230 - Successfully resolved class for [com.hello.market.server.MarketServerConfiguration]
2013-01-26 16:05:33,589 DEBUG org.springframework.web.context.support.AnnotationConfigWebApplicationContext.obtainFreshBeanFactory:530 - Bean factory for Root WebApplicationContext: org.springframework.beans.factory.support.DefaultListableBeanFactory@18b3364: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,marketServerConfiguration]; root of factory hierarchy
2013-01-26 16:05:33,619 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2013-01-26 16:05:33,619 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2013-01-26 16:05:33,666 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
2013-01-26 16:05:33,668 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2013-01-26 16:05:33,749 DEBUG org.springframework.web.context.support.StandardServletEnvironment.addLast:104 - Adding [class path resource [marketservice.properties]] PropertySource with lowest search precedence
2013-01-26 16:05:33,761 DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod:270 - Registering bean definition for @Bean method com.hello.market.server.MarketServerConfiguration.equityService()
2013-01-26 16:05:33,762 DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod:270 - Registering bean definition for @Bean method com.hello.market.server.MarketServerConfiguration.equityServiceEndPoint()
2013-01-26 16:05:33,908 DEBUG org.springframework.context.annotation.ConfigurationClassEnhancer.enhance:111 - Successfully enhanced com.hello.market.server.MarketServerConfiguration; enhanced class name is: com.hello.market.server.MarketServerConfiguration$$EnhancerByCGLIB$$f3beb75c
2013-01-26 16:05:33,909 DEBUG org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses:339 - Replacing bean definition 'marketServerConfiguration' existing class name 'com.hello.market.server.MarketServerConfiguration' with enhanced class name 'com.hello.market.server.MarketServerConfiguration$$EnhancerByCGLIB$$f3beb75c'
2013-01-26 16:05:33,916 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2013-01-26 16:05:33,916 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2013-01-26 16:05:33,917 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
2013-01-26 16:05:33,918 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2013-01-26 16:05:33,919 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2013-01-26 16:05:33,919 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2013-01-26 16:05:33,920 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
2013-01-26 16:05:33,920 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2013-01-26 16:05:33,921 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2013-01-26 16:05:33,921 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2013-01-26 16:05:33,924 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
2013-01-26 16:05:33,924 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2013-01-26 16:05:33,925 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2013-01-26 16:05:33,925 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2013-01-26 16:05:33,925 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor' to allow for resolving potential circular references
2013-01-26 16:05:33,926 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2013-01-26 16:05:33,926 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
2013-01-26 16:05:33,926 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
2013-01-26 16:05:33,926 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0' to allow for resolving potential circular references
2013-01-26 16:05:33,927 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
2013-01-26 16:05:33,930 DEBUG org.springframework.web.context.support.AnnotationConfigWebApplicationContext.initMessageSource:799 - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@10721b0]
2013-01-26 16:05:33,934 DEBUG org.springframework.web.context.support.AnnotationConfigWebApplicationContext.initApplicationEventMulticaster:823 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@13c7378]
2013-01-26 16:05:33,938 DEBUG org.springframework.ui.context.support.UiApplicationContextUtils.initThemeSource:85 - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@147c1db]
2013-01-26 16:05:33,940 INFO org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons:581 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18b3364: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,marketServerConfiguration,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0,equityService,equityServiceEndPoint]; root of factory hierarchy
2013-01-26 16:05:33,940 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2013-01-26 16:05:33,940 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2013-01-26 16:05:33,940 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2013-01-26 16:05:33,940 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2013-01-26 16:05:33,941 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2013-01-26 16:05:33,941 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'marketServerConfiguration'
2013-01-26 16:05:33,941 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'marketServerConfiguration'
2013-01-26 16:05:33,945 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'marketServerConfiguration' to allow for resolving potential circular references
2013-01-26 16:05:33,958 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'marketServerConfiguration'
2013-01-26 16:05:33,958 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0'
2013-01-26 16:05:33,959 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'equityService'
2013-01-26 16:05:33,959 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'equityService'
2013-01-26 16:05:33,965 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'marketServerConfiguration'
2013-01-26 16:05:34,012 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'equityService' to allow for resolving potential circular references
2013-01-26 16:05:34,015 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'equityService'
2013-01-26 16:05:34,015 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.getSingleton:217 - Creating shared instance of singleton bean 'equityServiceEndPoint'
2013-01-26 16:05:34,016 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:430 - Creating instance of bean 'equityServiceEndPoint'
2013-01-26 16:05:34,016 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'marketServerConfiguration'
2013-01-26 16:05:34,017 DEBUG org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext:89 - Current WebApplicationContext is not available for processing of EquityServiceEndPoint: Make sure this class gets constructed in a Spring web application. Proceeding without injection.
2013-01-26 16:05:34,024 DEBUG org.springframework.beans.factory.annotation.InjectionMetadata.<init>:60 - Found injected element on class [com.hello.market.server.service.soap.EquityServiceEndPoint]: AutowiredFieldElement for private com.hello.market.server.service.EquityService com.hello.market.server.service.soap.EquityServiceEndPoint.equityService
2013-01-26 16:05:34,024 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doCreateBean:504 - Eagerly caching bean 'equityServiceEndPoint' to allow for resolving potential circular references
2013-01-26 16:05:34,028 DEBUG org.springframework.beans.factory.annotation.InjectionMetadata.inject:85 - Processing injected method of bean 'equityServiceEndPoint': AutowiredFieldElement for private com.hello.market.server.service.EquityService com.hello.market.server.service.soap.EquityServiceEndPoint.equityService
2013-01-26 16:05:34,031 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'equityService'
2013-01-26 16:05:34,031 DEBUG org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.registerDependentBeans:424 - Autowiring by type from bean name 'equityServiceEndPoint' to bean named 'equityService'
2013-01-26 16:05:34,034 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.createBean:458 - Finished creating instance of bean 'equityServiceEndPoint'
2013-01-26 16:05:34,037 DEBUG org.springframework.web.context.support.AnnotationConfigWebApplicationContext.initLifecycleProcessor:850 - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@55a338]
2013-01-26 16:05:34,038 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBean:245 - Returning cached instance of singleton bean 'lifecycleProcessor'
2013-01-26 16:05:34,039 DEBUG org.springframework.web.context.ContextLoader.initWebApplicationContext:296 - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2013-01-26 16:05:34,039 INFO org.springframework.web.context.ContextLoader.initWebApplicationContext:301 - Root WebApplicationContext: initialization completed in 707 ms
2013-01-26 16:05:34,104 INFO org.mortbay.log.info:67 - Started SocketConnector@0.0.0.0:8888
编辑:我发现如果我执行以下操作,Web 服务将起作用:
@Configuration
@ImportResource("classpath:/ws-context.xml")
public class MarketServerConfiguration {
在 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.0.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">
<!-- Temperature Web Service -->
<wss:binding url="/service/EquityService">
<wss:service>
<ws:service bean="#equityServiceEndPoint">
</ws:service>
</wss:service>
</wss:binding>
</beans>
现在,如果我访问 localhost:8888/service/EquityService?wsdl,我将为我打印出 WSDL。
所以这是使它工作的一种方法,但是,我想这样做而不必使用那个额外的 XML 文件。有没有办法做到这一点?