2012-09-23 14 views
6

Xin chào Tôi đã thiết lập và chạy webservice, tôi đã sử dụng jax ws. Tôi đã sử dụng Spring để có thể sử dụng bean với Autowired và các công cụ mà spring cung cấp như là giá trị thuộc tính injection trong applicationContext.xml.Dịch vụ web JAX WS không lấy bean mùa xuân từ ngữ cảnh ứng dụng, do đó ném ngoại lệ con trỏ null

Tôi có sự xâm nhập xuân applicationcontext.xml dưới đây:

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

Trong web lớp điểm cuối dịch vụ, tôi đã làm:

@Autowired private MyBeanProperty myProperty; 

Và tôi có một phương pháp:

public String getSize() { 

return myProperty.getSize(); 

} 

Thật không may khi tôi gọi phương thức nó không nhận được bất kỳ giá trị nào và ném nullpointerexception.

PS: Tôi đã sử dụng soapUI để chạy wsdl của webservice và gọi phương thức.

Dịch vụ web có chạy trước khi các bean được tạo bởi Spring không ??


Để duffmo

Có tôi sử dụng quét thành phần trong applicationContext. Và tôi có trình lắng nghe bộ tải ngữ cảnh như bên dưới trong tệp web.xml. Xin hãy giúp tôi ..

Đây là explainaton mã hoàn chỉnh của tôi với mã

Tôi đang sử dụng JAX-WS và mùa xuân và cố gắng thiết lập một vài WebServices cần chạy trên Tomcat 7. Tôi đang sử dụng Maven như xây dựng công cụ do đó tôi chỉ liệt kê hai phụ thuộc của tôi ở đây:

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

lớp dịch vụ của tôi được đặt tại com.test.services và được đặt tên TestService & HelloWorldService và tìm kiếm như sau:

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"; 
    } 

} 

đây là web.xml của tôi:

<?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> 

và đây là của tôi nắng 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> 

này hoạt động tuyệt vời và tôi có thể truy cập vào dịch vụ bằng cách chỉ trình duyệt của tôi tới [ url] http://localhost:8080/toolbox/testservice[/url] tương ứng [url] http://localhost:8080/toolbox/helloworldservice[/url]. Tuy nhiên hỗ trợ mùa xuân rõ ràng là không được kích hoạt.

tôi đã cố gắng sau đó chỉ để lại di các HelloWorldService sẵn: 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> 

và 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> 

hơn nữa tôi chú thích cả hai lớp dịch vụ với @Service chú thích. Như tôi đã đề cập trước đây, điều này chỉ xuất bản webservice đầu tiên theo thứ tự bảng chữ cái, do đó HelloWorldService. Đồng thời thay đổi URL, vì dịch vụ hiện có sẵn dưới dạng [url] http://localhost:8080/[/url] thay vì [url] http://localhost:8080/toolbox/helloworldservice[/url]. Việc ghi nhật ký các chương trình Tomcat, rằng bối cảnh mùa xuân tải cả hai lớp là các bean Spring. Bạn có bất kỳ ý tưởng hoặc đề xuất nào về cách bật hỗ trợ Spring trong khi vẫn giữ cả hai dịch vụ có sẵn không ??

Trả lời

0

Tôi nghĩ rằng nhiều khả năng ngữ cảnh mùa xuân của bạn chưa được tải và cung cấp cho dịch vụ web. Làm thế nào bạn đã làm điều đó?

Bạn cần có ContextLoaderListener được định cấu hình trong web.xml cho WAR mà dịch vụ web được triển khai. Bạn đã cho biết nơi tải ngữ cảnh mùa xuân? Bạn đang sử dụng quét thành phần?

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

2

TestService của bạn (được chú thích với @WebService) nên mở rộng "SpringBeanAutowiringSupport" class để kick bắt đầu mùa xuân ràng buộc.

Vui lòng xem xét các điểm duffmo được đề cập.

+0

Giờ đây, nó đã hoạt động rất tốt, cảm ơn. @Autowired vẫn không hoạt động nhưng công việc quét thành phần, vì vậy tôi đã làm một công việc xung quanh bằng cách sử dụng ngữ cảnh Ứng dụng và gọi phương thức getBean (""). – Steer360

5

Không cần phải sử dụng ApplicationContext là tốt, nếu bạn làm những điều sau đây

  1. Chú thích lớp dịch vụ của bạn với @Service
  2. lớp dịch vụ nên kéo dài "SpringBeanAutowiringSupport".

Vui lòng xem đoạn mã sau đây.

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

Điều này sẽ tạo ra hạt giống như một singleton hoặc một đối tượng theo yêu cầu? – Zeus

+1

Nó sẽ tạo ra đối tượng đơn lẻ – Hemanth

3

Tôi có cùng một vấn đề, để giải quyết nó, tôi bắt đầu jaxws người nghe sau khi nghe mùa xuân:

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

Hy vọng nó giúp

6

It is answered here. Cuối cùng không có gì làm việc khác ngoài việc thêm mã bên dưới vào dịch vụ impl.

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

Đây phải là câu trả lời được chấp nhận. – vault

Các vấn đề liên quan