编程语言
首页 > 编程语言> > 解决webservice(Java)中dao层注入为null问题

解决webservice(Java)中dao层注入为null问题

作者:互联网

首先在webservice指定发布的路径类中实现 ServletContextListener,

例如:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ILockService implements ServletContextListener{
  
static IUserRecordService userRecordService; @Override public void contextInitialized(ServletContextEvent sce) { userRecordService = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(IUserRecordService.class); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } public String invoke(String xmlData) { //解决该处userRecordService为null的问题 UserRecord user = userRecordService.queryUserById(Id); } }

然后在web.xml中监听该类,添加如下:

<listener>
    <description>ILockService</description>
    <listener-class>com.cn.hnust.webservice.server.ILockService</listener-class>
</listener>

好的,这样就可以搞定了!

标签:userRecordService,ServletContextEvent,Java,webservice,dao,ILockService,ServletCo
来源: https://www.cnblogs.com/qianyou304/p/11549697.html