Веб-сервис JAX WS не принимает Spring Bean из applicationcontext, поэтому выдает исключение нулевого указателя
Привет я получил веб-сервис и работает, я использовал jax ws. Я использовал Spring, чтобы иметь возможность использовать bean-компоненты с Autowired и другие вещи, которые Spring выдает как инъекцию значения свойства в applicationContext.xml.
У меня есть следующая запись весны 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();
}
К сожалению, когда я вызываю метод, он не получает никакого значения и выбрасывает nullpointerexception.
PS: я использовал soapUI для запуска wsdl веб-сервиса и вызвал метод.
Работает ли веб-служба до того, как Spring создаст bean-компоненты?
Даффмо
Да, я использовал компонентное сканирование в applicationContext. И у меня есть слушатель загрузчика контекста, как показано ниже в web.xml. Пожалуйста, помогите мне..
Вот мое полное объяснение кода с кодом
Я использую JAX-WS и Spring и пытаюсь настроить несколько веб-сервисов, которые должны работать на Tomcat 7. Я использую 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: // локальный: 8080 / набор инструментов / TestService [/ URL] соответственно [url]HTTP: // локальный: 8080 / набор инструментов / helloworldservice [/ URL]. However Spring support is obviously not activated.
Я попробовал следующее, которое просто делает доступным 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. Как я упоминал ранее, здесь публикуется только первый алфавитный веб-сервис, поэтому HelloWorldService. Также он меняет URL, так как сервис теперь доступен как [url]HTTP: // локальный: 8080 / [/ URL] а не [URL]HTTP: // локальный: 8080 / набор инструментов / 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??