当前位置: 首页 > 工具软件 > JAX-WS > 使用案例 >

SpringBoot使用jax-ws开发WebService接口 使用@Autowired空指针异常

白烨煜
2023-12-01

解决思路:
1、初始化时把WebApplicationContext存下来
2、参考SpringBeanAutowiringSupport 把WebApplicationContext 拿出来,并使用


第一步:创建一个工具类,继承ServletContextInitializer类,重写里边的onStartup方法。

@Configuration
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {

    private static WebApplicationContext webApplicationContext;

    public static WebApplicationContext getCurrentWebApplicationContext() {
        return webApplicationContext;
    }

    /**
     * 在启动时将servletContext 获取出来,后面再读取二次使用。
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }
}

第二步:创建一个抽象类,作用等同于SpringBeanAutowiringSupport( 解决配置文件上下文和beanFactory工厂加载顺序导致autowire为空的异常),区别是获取到的WebApplicationContext是我们自己保存下来的。


import cn.hutool.core.lang.Assert;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import me.zhengjie.config.WebApplicationContextLocator;
import nl.basjes.shaded.org.springframework.util.ClassUtils;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.web.context.WebApplicationContext;

public abstract class SpringBootBeanAutowiringSupport {

    private static final Log logger = LogFactory.getLog(SpringBootBeanAutowiringSupport.class);


    /**
     * This constructor performs injection on this instance,
     * based on the current web application context.
     * <p>Intended for use as a base class.
     * @see #processInjectionBasedOnCurrentContext
     */
    public SpringBootBeanAutowiringSupport() {
        processInjectionBasedOnCurrentContext(this);
    }


    /**
     * Process {@code @Autowired} injection for the given target object,
     * based on the current web application context.
     * <p>Intended for use as a delegate.
     * @param target the target object to process
     * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
     */
    public static void processInjectionBasedOnCurrentContext(Object target) {
        Assert.notNull(target, "Target object must not be null");
        //注意此处要调用自己第一步创建的WebApplicationContextLocator
        WebApplicationContext cc = WebApplicationContextLocator.getCurrentWebApplicationContext();
        if (cc != null) {
            AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
            bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
            bpp.processInjection(target);
        }
        else {
            if (logger.isDebugEnabled()) {
                logger.debug("Current WebApplicationContext is not available for processing of " +
                        ClassUtils.getShortName(target.getClass()) + ": " +
                        "Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
            }
        }
    }
}

结论
jax-ws 或类似的应用,直接使用改造后的AutowiringSupport即可
extends SpringBootBeanAutowiringSupport

原文地址

 类似资料: