gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:20:22 33 4
gpt4 key购买 nike

嗨,我已经启动并运行了 web 服务,我使用了 jax ws。我已经使用 Spring 来使用带有 Autowired 的 bean 和 spring 提供的东西,比如 applicationContext.xml 中的属性值注入(inject)。

我有以下 spring applicationcontext.xml 条目:

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

在 Web 服务端点类中,我完成了:
@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 可用:
网页.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 支持有任何想法或建议吗?

最佳答案

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

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

关于spring - JAX WS webservice不从applicationcontext中获取spring bean,因此抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555707/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com