靜態工具類中使用註解注入service

通常須要在一個工具類中使用@Autowired 註解注入一個service。可是因爲工具類方法通常都寫成static,因此直接注入就存在問題。 java

使用以下方式能夠解決:
spring

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package cn.ffcs.drive.common.util;  
  5.   
  6. import javax.annotation.PostConstruct;  
  7. import javax.servlet.http.HttpServletRequest;  
  8.   
  9. import org.slf4j.Logger;  
  10. import org.slf4j.LoggerFactory;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12. import org.springframework.stereotype.Component;  
  13.   
  14. import cn.ffcs.drive.domain.Admin;  
  15. import cn.ffcs.drive.domain.OpeLog;  
  16. import cn.ffcs.drive.service.IOpeLogService;  
  17. import cn.ffcs.zq.util.DateUtils;  
  18.   
  19. /** 
  20.  * className:OpeLogUtils 
  21.  *  
  22.  * 管理員操做日誌 
  23.  *  
  24.  * @author pengyh 
  25.  * @version 1.0.0 
  26.  * @date 2014-07-10 09:04:48 
  27.  *  
  28.  */  
  29. @Component  
  30. public class OpeLogUtils {  
  31.   
  32.     private static Logger logger = LoggerFactory.getLogger(OpeLogUtils.class);  
  33.   
  34.     @Autowired  
  35.     private IOpeLogService opeLogService;  
  36.     private static OpeLogUtils opeLogUtils;  
  37.   
  38.     public void setUserInfo(IOpeLogService opeLogService) {  
  39.         this.opeLogService = opeLogService;  
  40.     }  
  41.       
  42.     @PostConstruct  
  43.     public void init() {  
  44.         opeLogUtils = this;  
  45.         opeLogUtils.opeLogService = this.opeLogService;  
  46.   
  47.     }  
  48.   
  49.     /** 
  50.      * 執行操做日誌入庫操做 
  51.      * @param adminId   管理員id 
  52.      * @param opeDesc   操做日誌信息 
  53.      * @param cityCode  城市編碼 
  54.      */  
  55.     public static void insertOpeLog(HttpServletRequest req, String opeDesc) {  
  56.         try {  
  57.             /** 
  58.              * 獲取管理員信息 
  59.              */  
  60.             Admin admin = DriveUtil.getSessionUser(req);  
  61.               
  62.             if(admin != null && opeDesc != null && !opeDesc.trim().equals("")){  
  63.                   
  64.                 //封裝日誌信息  
  65.                 logger.info("開始封裝日誌信息。");  
  66.                 OpeLog opeLog = new OpeLog();  
  67.                   
  68.                 opeLog.setAdminId(admin.getId());  
  69.                 opeLog.setCityCode(admin.getCityCode());  
  70.                 opeLog.setOpeDesc("管理員id="+admin.getId()+"操做【"+opeDesc+"】");  
  71.                 opeLog.setOpeTime(DateUtils.getNow());  
  72.                 opeLog.setIsDelete("0");  
  73.                 opeLogUtils.opeLogService.save(opeLog);  
  74.                   
  75.                 logger.info("保存管理員操做日誌成功,信息爲【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null,admin.getCityCode(),opeDesc,DateUtils.getNow()});  
  76.             }else{  
  77.                 logger.info("保存操做日誌失敗,參數不足【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null, admin!=null?admin.getCityCode():null, opeDesc, DateUtils.getNow()});  
  78.             }  
  79.         } catch (Exception e) {  
  80.             logger.error("保存操做日誌異常,異常信息爲:" + e.getMessage(), e);  
  81.         }  
  82.     }  
  83.   
相關文章
相關標籤/搜索