如果直接按照业务层方式,在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层的接口就不会为空,能通过调用方法来访问数据库之类的