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

使用如下方式可以解决:

  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);

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

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

    转载:http://blog.csdn.net/p793049488/article/details/37819121 一般需要在一个工具类中使用@Autowired 注解注入一个service.但是 ...

  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. window服务 调试步骤

    方法一: 1.编译windows服务项目工程 2.把服务注册到系统服务上 3.在visual studio 编辑器中,打断点,用 Debug  进程调试 方法二: 在Onstart 方法中,加上 De ...

  2. 关于Control.Dispatcher.BeginInvoke卡界面

    Control.Dispatcher.BeginInvoke里的逻辑由UI线程执行,如果内部包含耗时操作就会造成界面卡住. Action.BeginInvoke里的逻辑,将在一个新开的线程中执行,而不 ...

  3. Spring的校验(Validator)

    使用Spring校验的大体流程: 最首先要有配置文件xml的支持(spring_validate.xml).(当然在web.xml中要有对该xml的体现) <beans xmlns=" ...

  4. 复习-PEP8规范(转)

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换 ...

  5. 基于socketserver实现的并发(tcp和udp)

    threading 线程 基于tcp协议:请求建立连接,然后开启进程 基于udp协议:直接开启新进程 基于tcp协议 import socketserver # 导入socketserver模块 # ...

  6. python中一些函数应用

    items将一个字典以列表的形式返回,因为字典是无序的,所以返回的列表也是无序的. 例如:a = {"a":1,"b":2}    a.items  就是 a ...

  7. Vue 2.0入门基础知识之全局API

    3.全局API 3-1. Vue.directive 自定义指令 Vue.directive用于自定义全局的指令 实例如下: <body> <div id="app&quo ...

  8. 一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用

    这个命令行神器的下载地址:https://stedolan.github.io/jq/# Windows和Linux版本均只有两个可执行文件,大小不过2MB多. 以Windows版本为例,介绍其用法. ...

  9. 原创:Nginx反向代理实战部署

    均衡负载服务器 10.0.0.9 [root@web03 conf]# vim nginx.conf worker_processes  1; events { worker_connections  ...

  10. QT +菜单栏和工具栏

    #include "mainwindow.h" #include <QMenuBar>//菜单栏需要的头文件 #include <QMenu>//菜单 #i ...