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

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

注:Spring工厂要有这个bean。

使用如下方式可以解决:

  1. /**
  2. *
  3. */
  4. package cn.ffcs.drive.common.util;
  5. import javax.annotation.PostConstruct;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. import cn.ffcs.drive.domain.Admin;
  12. import cn.ffcs.drive.domain.OpeLog;
  13. import cn.ffcs.drive.service.IOpeLogService;
  14. import cn.ffcs.zq.util.DateUtils;
  15. /**
  16. * className:OpeLogUtils
  17. *
  18. * 管理员操作日志
  19. *
  20. * @author pengyh
  21. * @version 1.0.0
  22. * @date 2014-07-10 09:04:48
  23. *
  24. */
  25. @Component
  26. public class OpeLogUtils {
  27. private static Logger logger = LoggerFactory.getLogger(OpeLogUtils.class);
  28. @Autowired
  29. private IOpeLogService opeLogService;
  30. private static OpeLogUtils opeLogUtils;
  31. public void setUserInfo(IOpeLogService opeLogService) {
  32. this.opeLogService = opeLogService;
  33. }
  34. @PostConstruct
  35. public void init() {
  36. opeLogUtils = this;
  37. opeLogUtils.opeLogService = this.opeLogService;
  38. }
  39. /**
  40. * 执行操作日志入库操作
  41. * @param adminId   管理员id
  42. * @param opeDesc   操作日志信息
  43. * @param cityCode  城市编码
  44. */
  45. public static void insertOpeLog(HttpServletRequest req, String opeDesc) {
  46. try {
  47. /**
  48. * 获取管理员信息
  49. */
  50. Admin admin = DriveUtil.getSessionUser(req);
  51. if(admin != null && opeDesc != null && !opeDesc.trim().equals("")){
  52. //封装日志信息
  53. logger.info("开始封装日志信息。");
  54. OpeLog opeLog = new OpeLog();
  55. opeLog.setAdminId(admin.getId());
  56. opeLog.setCityCode(admin.getCityCode());
  57. opeLog.setOpeDesc("管理员id="+admin.getId()+"操作【"+opeDesc+"】");
  58. opeLog.setOpeTime(DateUtils.getNow());
  59. opeLog.setIsDelete("0");
  60. opeLogUtils.opeLogService.save(opeLog);
  61. logger.info("保存管理员操作日志成功,信息为【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null,admin.getCityCode(),opeDesc,DateUtils.getNow()});
  62. }else{
  63. logger.info("保存操作日志失败,参数不足【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null, admin!=null?admin.getCityCode():null, opeDesc, DateUtils.getNow()});
  64. }
  65. } catch (Exception e) {
  66. logger.error("保存操作日志异常,异常信息为:" + e.getMessage(), e);
  67. }
  68. }
  69. }

静态工具类中使用注解注入service的更多相关文章

  1. 静态工具类中使用注解注入service实例

    一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package ...

  2. spring 在静态工具类中使用注解注入bean

    /** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件 ...

  3. SpringBoot 项目在静态工具类中注入 RedisTemplate

    静态属性不能直接注入,可以通过其set方法进行注入.(注意生成的set方法需要去掉static). 在工具类里直接注入RedisTemplate,两种方法: (1)使用@Autowired priva ...

  4. 解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的s ...

  5. JAVA中SpringMVC获取bean方法,在工具类(utils)注入service

    有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以 通过contextLoader获取 WebApplicationContext ctx = ContextLoade ...

  6. 在静态工具类中需要注入mapper

    在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" ini ...

  7. Spring 中将service 注入到普通的工具类中

    记录一下,防止忘记. 要求每次生成一个和数据库不重复的组队码,于是就想在工具类中加入service注入 方法1(红框是注意的地方)

  8. Springboot在工具类(Util)中使用@Autowired注入Service

    1. 使用@Component注解标记工具类MailUtil: 2. 使用@Autowired注入我们需要的bean: 3. 在工具类中编写init()函数,并使用@PostConstruct注解标记 ...

  9. 关于工具类静态方法调用@Autowired注入的service类问题

    @Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...

随机推荐

  1. zoj 1648 判断线段是否相交

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648 Circuit Board Time Limit: 2 Second ...

  2. ubuntu的dns设置

    ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s

  3. HDU 1394

    单点,利用线段树解题,看到数据大小一定要敏感,说不定就是暗藏的解题思路 #include <stdio.h> #define lson l,mid,id<<1 #define ...

  4. [转载] tmux 使用指南

    原文: https://danielmiessler.com/study/tmux/ tmux的用法和screen类似, 比screen好用一些, 不过需要单独安装

  5. jquery.pagination.js分页

    参数说明 参数名 描述 参数值 maxentries 总条目数                           必选参数,整数 items_per_page 每页显示的条目数            ...

  6. Java中List Set Map 是否有序等总结

    1.Collection List Set Map 区别记忆 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文 ...

  7. (四)802.1Q VLAN

  8. 使用火狐的restclient发送http接口post及get请求

    1.在firefox安装restclient插件,具体参照http://jingyan.baidu.com/article/1876c8529b07e3890b137623.html: —.发送pos ...

  9. ffmpeg,X264编码结果I帧QP比P帧还大

    enc_ctx->profile =FF_PROFILE_H264_MAIN ; enc_ctx->time_base.den = 24; enc_ctx->time_base.nu ...

  10. XML Attributes(XML属性)

    XML Attributes(XML属性) android:autoLink  是否自动链接网址或邮箱地址: android:autoText  自动检测错误: android:bufferType  ...