zoukankan      html  css  js  c++  java
  • 静态工具类中使用注解注入service

    转载:http://blog.csdn.net/p793049488/article/details/37819121

    一般需要在一个工具类中使用@Autowired 注解注入一个service。但是由于工具类方法一般都写成static,所以直接注入就存在问题。

    注:Spring工厂要有这个bean。

    使用如下方式可以解决:

     
    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. }  
  • 相关阅读:
    android技能树
    BitmapFactory 读取图片方法总结
    如何利用SecureCRT连接Ubuntu12.0.4
    Ubuntu 12.04如何从登录界面登录root
    ubuntu中vi在编辑状态下方向键不能用的解决
    在ubuntu12.0.4上搭建samba服务器以实现文件共享
    怎么利用ultraISO对一个文件夹制作ISO镜像
    VMware网络选项分析
    在vmware 6.5+ubuntu12.04上安装VMware tools出现问题的分析
    在ubuntu中获得root权限
  • 原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/5828442.html
Copyright © 2011-2022 走看看