依賴Spring的情況下,Java Web項目如何在啟動時加載數據庫中的數據?


原文:https://blog.csdn.net/u012345283/article/details/39558537

原文:https://blog.csdn.net/wandrong/article/details/44782627

我的嘗試:

1、繼承HttpServlet,在web.xml中配置servlet的<load-on-startup>1</load-on-startup>,項目啟動時globalParaService被注入了,但是跟蹤到init方法里面,globalParaService為null。(失敗)

public class InitSysServlet extends HttpServlet {

    private SysGlobalParaServiceI globalParaService;
    
    @Autowired
    public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
        this.globalParaService = globalParaService;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        SysGlobalPara codeRule = globalParaService.getCodeRule();
        if (null != codeRule) {
            SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
        } else {
            SystemConfig.IS_SET_WIZADED = false;
        }
        
    }
}

然后,我試着手動創建globalParaService,代碼調整為

public class InitSysServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        SysGlobalParaServiceI globalParaService = new SysGlobalParaServiceImpl();
        SysGlobalPara codeRule = globalParaService.getCodeRule();
        if (null != codeRule) {
            SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
        } else {
            SystemConfig.IS_SET_WIZADED = false;
        }
        
    }
}

然而,SysGlobalParaServiceImpl里面依賴的其他由Spring注入的對象為null。

搞不定了,百度到以上兩篇文章,根據上面文章進行嘗試都成功了。

2、直接在init方法中獲取Spring的bean (成功)

首先在Spring配置文件中配置SysGlobalParaServiceImpl類對應的bean,然后在代碼中獲取,最終代碼如下:

public class InitSysServlet extends HttpServlet {

    private SysGlobalParaServiceI globalParaService;
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();          
        globalParaService = (SysGlobalParaServiceI) wac.getBean("gpservice");

        SysGlobalPara codeRule = globalParaService.getCodeRule();
        if (null != codeRule) {
            SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
        } else {
            SystemConfig.IS_SET_WIZADED = false;
        }
    }
}

 

以下兩種則與servlet無關,而是繼承spring提供的接口。

3、實現Spring的接口InitializingBean,則實現類在被Sping初始化之后,會調用其中的afterPropertiesSet方法,代碼如下:

@Component
public class InitSysServlet implements InitializingBean {

    private SysGlobalParaServiceI globalParaService;
    
    @Autowired
    public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
        this.globalParaService = globalParaService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SysGlobalPara codeRule = globalParaService.getCodeRule();
        if (null != codeRule) {
            SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
        } else {
            SystemConfig.IS_SET_WIZADED = false;
        }
    }


}

4、實現Spring的接口ApplicationListener<ContextRefreshedEvent>,在所有的bean初始化之后會調用其中的方法onApplicationEvent,代碼如下:

@Component
public class InitSysServlet implements ApplicationListener<ContextRefreshedEvent> {

    private SysGlobalParaServiceI globalParaService;
    
    @Autowired
    public void setGlobalParaService(SysGlobalParaServiceI globalParaService) {
        this.globalParaService = globalParaService;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        SysGlobalPara codeRule = globalParaService.getCodeRule();
        if (null != codeRule) {
            SystemConfig.IS_SET_WIZADED = "true".equals(codeRule.getSysValue());
        } else {
            SystemConfig.IS_SET_WIZADED = false;
        }
    }

}

 

最后,我采用的是第4中,實現Spring的接口ApplicationListener<ContextRefreshedEvent>。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM