首页 技术 正文
技术 2022年11月23日
0 收藏 883 点赞 3,970 浏览 2061 个字

由于使用myeclipse自动生成的Delegate,所以在使用service实现层的时候,默认创建的时候都是使用new的方法;

这样就导致每一次请求过来都得new一个新的;如果service有注入其他的service时,就会出现null的情况;

出现该情况,有两种方法进行解决:

方法一,在serviceImpl实现成里面,使用的注入的service加一下null处理;

private ITestService testService;
private ITestService getTestService(){
  if(testervice==null){
    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    testService=(ITestService)context.getBean("testService");
  }
  return testService;}
public void setTestService(ITestService service) {
  this.testService = service;
}

方法二,添加Delegate类中添加注入(也可以在你创建的service加)

主要就3个地方:(代码标红的地方)

1、继承SpringBeanAutowiringSupport类;

2、Delegate实现类加注入@Controller;

3、在你需要注入的service加注入@Autowired

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;@WebService(
  targetNamespace = "http://service.com/",
  serviceName = "AuthCodeService",
  portName = "service/AuthCodeServicePort",
  wsdlLocation = "WEB-INF/wsdl/AuthCodeService.wsdl")
@Controller
public class AuthCodeServiceDelegate extends SpringBeanAutowiringSupport implements IAuthCodeServiceDelegate {

   // 这个是默认创建的
// com.service.AuthCodeService authCodeService = new com.service.AuthCodeService(); @Autowired
private IAuthSmsService authSmsService; public Result SendSmsCode(
@WebParam(name="inaccessInfo") InaccessInfo inaccessInfo,
@WebParam(name="record") Record record
) {
inaccessInfo.setAccessSeq(inaccessInfo.getAccessSeq()+","+getClientInfo());
return authSmsService.sendSmsCode(inaccessInfo, record);
} @Resource
private WebServiceContext wsContext;
/**
* 获取请求端IP地址 --(这个是赠送的)
* <p>2017-4-14 上午10:15:08</p>
*/
private String getClientInfo(){
MessageContext mc = wsContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest)(mc.get(MessageContext.SERVLET_REQUEST));
String remortAddress = request.getRemoteAddr();
return (remortAddress);
}
}

如发现其他方式,欢迎讨论~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866