【问题标题】:JAX WS webservice does not take spring bean from applicationcontext, hence throws null pointer exceptionJAX WS webservice不从applicationcontext中获取spring bean,因此抛出空指针异常
【发布时间】:2012-09-15 08:41:54
【问题描述】:

您好,我已经启动并运行了 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>

在网络服务端点类中,我已经完成了:

@Autowired private MyBeanProperty myProperty;

我有一个方法:

public String getSize() {

return myProperty.getSize();

}

不幸的是,当我调用该方法时,它没有得到任何值并抛出空指针异常。

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

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


到达夫莫

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

这是我的完整代码说明

我正在使用 JAX-WS 和 Spring,并尝试设置一些需要在 Tomcat 7 上运行的 WebService。 我使用 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 和 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 注释对两个服务类进行了注释。正如我之前提到的,这只会发布按字母顺序排列的第一个 web 服务,因此是 HelloWorldService。 它还更改了 URL,因为该服务现在以 [url]http://localhost:8080/[/url] 而不是 [url]http://localhost:8080/toolbox/helloworldservice[/url] 的形式提供。 Tomcat 的日志显示,Spring 上下文将两个类都加载为 Spring bean。 您对如何在保持两种服务可用的同时启用 Spring 支持有什么想法或建议吗?

【问题讨论】:

    标签: spring jax-ws


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      请查看 duffmo 提到的要点。

      【讨论】:

      • 现在运行良好,谢谢。 @Autowired 仍然没有工作,但组件扫描工作,所以我解决了使用应用程序上下文并调用 getBean("") 方法。
      【解决方案3】:

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

      1. 使用 @Service 注释您的服务类
      2. 服务类应该扩展“SpringBeanAutowiringSupport”。

      请看下面的sn-p。

      @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{
      

      【讨论】:

      • 这会将 bean 创建为单例还是每个请求的对象?
      • 它将创建单例对象
      【解决方案4】:

      我遇到了同样的问题,为了解决它,我在 spring 监听器之后启动了 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>
      

      希望对你有帮助

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          您错过了 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

          谢谢

          【讨论】:

            猜你喜欢
            • 2018-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多