解决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>

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

上一篇:【使用篇】SpringBoot整合Listener(三)


下一篇:vue2和vue3去掉地址栏里的#号