JAX WS Web服务未从applicationcontext中获取spring bean,因此引发了null指针异常 [英] JAX WS webservice does not take spring bean from applicationcontext, hence throws null pointer exception

查看:92
本文介绍了JAX WS Web服务未从applicationcontext中获取spring bean,因此引发了null指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经启动并运行了Web服务,我使用了jax ws.我已经使用Spring来将Bean与Autowired一起使用,并且Spring提供了诸如applicationContext.xml中的属性值注入之类的东西.

Hi I have got the webservice up and running , i have used jax ws. I have used Spring to be able to use beans with Autowired and stuff that spring gives like property value injection in applicationContext.xml.

我有下面的spring applicationcontext.xml条目:

I have the below spring applicationcontext.xml entry:

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

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

In web service end point class , i have done:

@Autowired private MyBeanProperty myProperty;

我有一个方法:

public String getSize() {

return myProperty.getSize();

}

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

Unfortunately when i invoke the method it does not get any value and throws nullpointerexception.

PS:我使用soapUI运行Web服务的wsdl并调用了该方法.

PS: I used soapUI to run the wsdl of the webservice and invoked the method.

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

Is the webservice runs before the beans get created by Spring??

致达菲(Duffmo)

To duffmo

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

Yes i used component scan in applicationContext. And i do have the context loader listener as below in web.xml. Please help me..

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

Here is my complete code explainaton with code

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

I am using JAX-WS and Spring and try to setup a few WebServices which need run on Tomcat 7. I am using Maven as build tool therefore I just list my two dependencies here:

<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,外观如下:

my service classes are located in com.test.services and are named TestService & HelloWorldService and look as follows:

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:

this is my 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:

and this is my 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支持.

This works great and I can access the services by pointing my browser to [url]http://localhost:8080/toolbox/testservice[/url] respectively [url]http://localhost:8080/toolbox/helloworldservice[/url]. However Spring support is obviously not activated.

我尝试了以下方法,仅使HelloWorldService可用: web.xml:

I tried the following which just leaver the HelloWorldService available: 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:

and 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支持同时保持两种服务都有任何想法或建议吗?

furthermore I annotated both Service classes with @Service annotation. As I mentioned before, this only publishes the alphabetically first webservice, hence HelloWorldService. Also it changes the URL, as the service is now available as [url]http://localhost:8080/[/url] rather than [url]http://localhost:8080/toolbox/helloworldservice[/url]. The logging of Tomcat shows, that the Spring Context loads both Classes as Spring beans. Do you have any ideas or suggestions on how to enable Spring support while keeping both services available??

推荐答案

It is answered here. Ultimately nothing worked other than adding below code to service impl.

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

这篇关于JAX WS Web服务未从applicationcontext中获取spring bean,因此引发了null指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆