You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JAX-WS调用出现空指针异常,@Autowired与Spring MVC整合求助

Fixing Null Pointer Exception with @Autowired in JAX-WS + Spring MVC Setup

Hey there! You're spot-on about the @Autowired dependency being the culprit here. Let me break down why this happens and walk you through practical solutions.

Why the Null Pointer Happens

JAX-WS web service endpoints are typically instantiated and managed by your application server (like Tomcat, WildFly) instead of the Spring container. That means Spring has no way to inject your autowired dependencies into the JAX-WS endpoint instance—so those dependencies end up being null, leading to the exception when you call the service method.

Solutions

1. Use Spring's SpringBeanAutowiringSupport (Simplest Approach)

This is the quickest fix, and it only requires the standard spring-web dependency you probably already have.

Add a @PostConstruct method to your JAX-WS service class to trigger Spring's injection manually:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.jws.WebService;
import javax.annotation.PostConstruct;

@WebService(endpointInterface = "com.yourpackage.YourServiceInterface")
public class YourServiceImpl implements YourServiceInterface {

    @Autowired
    private YourDependency yourDependency;

    @PostConstruct
    public void initSpringInjection() {
        // Tell Spring to inject dependencies into this instance
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    @Override
    public String yourServiceMethod() {
        // Now yourDependency won't be null!
        return yourDependency.performAction();
    }
}

Make sure your pom.xml includes the spring-web dependency (if it doesn't already):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>your-spring-version</version>
</dependency>

2. Manually Fetch Beans from Spring Context

If you prefer not to use SpringBeanAutowiringSupport, you can grab the Spring context from the servlet context and retrieve your dependency directly:

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

@WebService(endpointInterface = "com.yourpackage.YourServiceInterface")
public class YourServiceImpl implements YourServiceInterface {

    private YourDependency yourDependency;

    @Resource
    private WebServiceContext webServiceContext;

    @PostConstruct
    public void init() {
        MessageContext msgContext = webServiceContext.getMessageContext();
        ServletContext servletContext = (ServletContext) msgContext.get(MessageContext.SERVLET_CONTEXT);
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        yourDependency = appContext.getBean(YourDependency.class);
    }

    // Your service methods here
}

3. Full Spring Management of JAX-WS Endpoints (Advanced)

For a more integrated setup, you can let Spring manage your JAX-WS endpoints entirely using the jaxws-spring library.

First, add the dependency to your pom.xml (exclude conflicting Spring dependencies if needed):

<dependency>
    <groupId>org.jvnet.jax-ws-commons.spring</groupId>
    <artifactId>jaxws-spring</artifactId>
    <version>1.9</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </exclusion>
        <!-- Add other exclusions if your Spring version differs -->
    </exclusions>
</dependency>

Then configure your endpoint in your Spring application context file (e.g., applicationContext.xml):

<!-- Define your dependency bean -->
<bean id="yourDependency" class="com.yourpackage.YourDependencyImpl"/>

<!-- Define your service bean with injected dependency -->
<bean id="yourServiceBean" class="com.yourpackage.YourServiceImpl">
    <property name="yourDependency" ref="yourDependency"/>
</bean>

<!-- Configure the JAX-WS endpoint -->
<jaxws:endpoint 
    id="yourWebService" 
    implementor="#yourServiceBean" 
    address="/YourService"/>

With this setup, Spring creates and manages the endpoint instance, so @Autowired will work as expected without extra code in your service class.

Final Notes

The first approach is ideal for most cases—it's simple and doesn't require adding new dependencies. If you need deeper integration with Spring features, go for the third option.

内容的提问来源于stack exchange,提问作者vidarshana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:39:10