SpringBoot 开发@WebService接口使用jax-ws无法使用@Autowired

纪念一下,我在被这个问题折磨了3天之后,终于找到了解决方案!!!开心,万岁!!!
以下是我整理过后的。
网上很多都说让webService继承SpringBeanAutowiringSupport类就可以实现@WebService,但是Springboot项目行不通。
原因:

jax-ws 中bean的生命周期管理是不受spring 控制的。所以需要显式继承 SpringBeanAutowiringSupport。
		但是spring boot 的Servlet Context Initialization 有些不一样SpringBootServletInitializer.startup() 使用了自定义的ContextLoaderListener,
并没有把创建的rootAppContext传给ContextLoader。
所以JAX-WS endpoint 类初始化的时候, SpringBeanAutowiringSupport从ContextLoader.getCurrentWebApplicationContext()取值总是null,@Autowired当然就不能用了。
以下是源码
    public SpringBeanAutowiringSupport() {
        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");
        WebApplicationContext cc = ContextLoader.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.");
            }
        }
    }

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

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

@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是我们自己保存下来的。

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

第三步:将@webService类继承SpringBeanAutowiringSupport类就可以了。

@WebService
@Service
public class OrgCheckWebServiceImpl extends SpringBootBeanAutowiringSupport implements IOrgCheckWebService {

    @Autowired
    public OrgCheckMapper orgCheckMapper;

    @Autowired
    public OrganizationMapper organizationMapper;

    @Autowired
    public IOrganizationService organizationService;

    public static final Logger logger = LoggerFactory.getLogger(OrgCheckServiceImpl.class);
		
		//方法体

}

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

出处:https://www.jianshu.com/p/125258ada53b

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值