JAX WS webservice不从applicationcontext获取spring bean,因此抛出空指针异常

时间:2012-09-23 19:49:37

标签: spring jax-ws

您好我已经启动并运行了web服务,我使用了jax ws。我已经使用Spring来使用带有Autowired的bean和spring给出的类似于applicationContext.xml中的属性值注入的东西。

我有以下spring applicationcontext.xml条目:

<context:component-scan base-package="com.mybeans.service" />      
<bean  id="myProperty" class="com.mybeans.service.MyBeanProperty"
p:Size="BIG">
</bean>

在Web服务端点类中,我完成了:

@Autowired private MyBeanProperty myProperty;

我有一个方法:

public String getSize() {

return myProperty.getSize();

}

不幸的是,当我调用该方法时,它没有获得任何值并抛出nullpointerexception。

PS:我使用soapUI来运行webservice的wsdl并调用该方法。

web服务是否在Spring创建bean之前运行?


to duffmo

是的,我在applicationContext中使用了组件扫描。我确实有web.xml中的上下文加载器监听器。请帮帮我..

这是我的代码

的完整代码说明

我正在使用JAX-WS和Spring,并尝试设置一些需要在Tomcat 7上运行的WebServices。 我使用Maven作为构建工具,因此我只在这里列出我的两个依赖项:

<dependencies>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.0.5.RELEASE</version>
   </dependency>

     <dependencies>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-rt</artifactId>
      <version>2.1.3</version>
    </dependency>

    </dependencies>

我的服务类位于com.test.services中,名为TestService&amp; HelloWorldService,如下所示:

package com.test.services;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService( name = "Test", serviceName = "TestService" )
public class TestService {

  @WebMethod
  public String getTest() {
    return "Test";
  }

}

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>toolbox</display-name>
  <description>testing webservices</description>
  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>jaxws-servlet</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/testservice</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>jaxws-servlet</servlet-name>
    <url-pattern>/helloworldservice</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>10</session-timeout>
  </session-config>
</web-app>

这是我的sun-jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
    <endpoint
        name="jaxws-servlet"
        implementation="com.test.services.TestService"
        url-pattern="/testservice"/>
    <endpoint
        name="jaxws-servlet"
        implementation="com.test.services.HelloWorldService"
        url-pattern="/helloworldservice" />
</endpoints>

这很有效,我可以通过将浏览器分别指向[url] http://localhost:8080/toolbox/testservice[/url] [url] http://localhost:8080/toolbox/helloworldservice[/url]来访问服务。 但是Spring支持显然没有激活。

我尝试了以下只提供可用的HelloWorldService: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>toolbox</display-name>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

和applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemaLocation="
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  <context:component-scan base-package="com.test.services" />
  <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
    <property name="baseAddress" value="http://localhost:8080/" />
  </bean>
 </beans>

此外,我使用@Service注释注释了两个Service类。正如我之前提到的,这只是按字母顺序发布第一个web服务,因此发布了HelloWorldService。 此外,它还会更改网址,因为该服务现在可用[url] http://localhost:8080/[/url]而不是[url] http://localhost:8080/toolbox/helloworldservice[/url]。 Tomcat的日志记录显示,Spring Context将两个类加载为Spring bean。 对于如何在保持两种服务可用的同时启用Spring支持,您有什么想法或建议吗?

6 个答案:

答案 0 :(得分:6)

It is answered here。除了将以下代码添加到服务impl之外,最终没有任何效果。

    @PostConstruct
public void init() {
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}

答案 1 :(得分:5)

如果您执行以下操作,则无需使用ApplicationContext

  1. 使用@Service
  2. 注释您的服务类
  3. 服务类应该扩展“SpringBeanAutowiringSupport”。
  4. 请查看以下代码段。

    @org.springframework.stereotype.Service
    @javax.jws.WebService (endpointInterface="a.b.c.MyPort", 
                targetNamespace="http://a.b.co.in/Retail/MyService/V1", 
                serviceName="MyService", 
                portName="MyServicePort", 
                wsdlLocation="wsdl/MyService.wsdl")
    public class MyServiceBindingImpl extends org.springframework.web.context.support.SpringBeanAutowiringSupport{
    

答案 2 :(得分:3)

我遇到了同样的问题,要解决它我在spring listener之后启动了jaxws监听器:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>

希望有所帮助

答案 3 :(得分:2)

您的TestService(使用@WebService注释)应该扩展“SpringBeanAutowiringSupport”类以启动弹簧绑定。

请看看duffmo提到的要点。

答案 4 :(得分:0)

我认为您的Spring上下文更有可能无法加载并可供Web服务使用。你是怎么做到的?

您应该在ContextLoaderListener中为部署了Web服务的WAR配置web.xml。你有没有告诉它在哪里加载Spring上下文?你在使用组件扫描吗?

http://renidev.wordpress.com/2009/02/02/how-to-use-springs-context-component-scan-and-annotation/

答案 5 :(得分:0)

您错过了Web服务注入的配置。因此,将更多内容放入web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

请不要忘记订单。因为您需要先为自动装配字段初始化bean

谢谢