SpringBoot 发布webservice接口,实现接口如何调用业务层代码

如果直接按照业务层方式,在webservice实现是不可行的,@Autowired无法自动注入,还会报空指针的错误,因为在webservice的自动注入不是在spring容器中找bean对象,所以按照service层方式是无法取得对象,所以我找到一篇文章,里面教我如何手动给webservice注入我们所需要的对象,例如我们想调用service层的方法,那就把service层的接口找出来,并手动注入,放在webservice的实现类,然后再调用方法。
手动调用工具类如下:

package com.xgs.hisystem.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring容器工具类
 * @author lv
 *
 */
@Component("springContextUtils")
public class SpringContextUtils implements ApplicationContextAware {
	/**
	 * Spring应用上下文环境
	 */
	private static ApplicationContext applicationContext;

	/**
	 * 实现ApplicationContextAware接口的回调方法,设置上下文环境
	 *
	 * @param applicationContext
	 */
	public void setApplicationContext(ApplicationContext applicationContext) {
		SpringContextUtils.applicationContext = applicationContext;
	}

	/**
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 获取对象
	 *
	 * @param name
	 * @return Object
	 * @throws BeansException
	 */
	public static Object getBean(String name) throws BeansException {
		return applicationContext.getBean(name);
	}

	/**
	 * 根据class获取对应的bean对象
	 * @param clz
	 * @return
	 */
	public static Object getBean(Class<?> clz){
		return applicationContext.getBean(clz);
	}
}



在我的项目中,是这样调用的

在这里插入图片描述

@WebService(endpointInterface = "com.xgs.hisystem.webservice.TripartWebService",targetNamespace = "http://webservice.hisystem.xgs.com/")
public class TripartWebServiceImpl implements TripartWebService {

    IRegisterService iRegisterService=(IRegisterService) SpringContextUtils.getBean(IRegisterService.class);

    private UserEntity userEntity=new UserEntity();
    @Override
    public String queryDoctor() {
        return iRegisterService.queryDoctor();
    }
}

这样调用的话,里面的service层的接口就不会为空,能通过调用方法来访问数据库之类的

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值