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

Ste*_*360 6 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>
Run Code Online (Sandbox Code Playgroud)

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

@Autowired private MyBeanProperty myProperty;
Run Code Online (Sandbox Code Playgroud)

我有一个方法:

public String getSize() {

return myProperty.getSize();

}
Run Code Online (Sandbox Code Playgroud)

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

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

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


为了duffmo

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

这是我完整的代码解释代码

我正在使用JAX-WS和Spring并尝试设置一些需要在Tomcat 7上运行的Web服务.我使用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>
Run Code Online (Sandbox Code Playgroud)

我的服务类位于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";
  }

}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这很好用,我可以通过将浏览器分别指向[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>
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

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

小智 6

在这里回答.除了在服务impl中添加以下代码之外,最终没有任何效果.

    @PostConstruct
public void init() {
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
Run Code Online (Sandbox Code Playgroud)


Hem*_*nth 5

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

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

请查看以下代码段.

@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{
Run Code Online (Sandbox Code Playgroud)


duf*_*ymo 0

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

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

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