静态工具类中使用注解注入service
转载:http://blog.csdn.net/p793049488/article/details/37819121
一般需要在一个工具类中使用@Autowired 注解注入一个service。但是由于工具类方法一般都写成static,所以直接注入就存在问题。
注:Spring工厂要有这个bean。
使用如下方式可以解决:
- /**
- *
- */
- package cn.ffcs.drive.common.util;
- import javax.annotation.PostConstruct;
- import javax.servlet.http.HttpServletRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import cn.ffcs.drive.domain.Admin;
- import cn.ffcs.drive.domain.OpeLog;
- import cn.ffcs.drive.service.IOpeLogService;
- import cn.ffcs.zq.util.DateUtils;
- /**
- * className:OpeLogUtils
- *
- * 管理员操作日志
- *
- * @author pengyh
- * @version 1.0.0
- * @date 2014-07-10 09:04:48
- *
- */
- @Component
- public class OpeLogUtils {
- private static Logger logger = LoggerFactory.getLogger(OpeLogUtils.class);
- @Autowired
- private IOpeLogService opeLogService;
- private static OpeLogUtils opeLogUtils;
- public void setUserInfo(IOpeLogService opeLogService) {
- this.opeLogService = opeLogService;
- }
- @PostConstruct
- public void init() {
- opeLogUtils = this;
- opeLogUtils.opeLogService = this.opeLogService;
- }
- /**
- * 执行操作日志入库操作
- * @param adminId 管理员id
- * @param opeDesc 操作日志信息
- * @param cityCode 城市编码
- */
- public static void insertOpeLog(HttpServletRequest req, String opeDesc) {
- try {
- /**
- * 获取管理员信息
- */
- Admin admin = DriveUtil.getSessionUser(req);
- if(admin != null && opeDesc != null && !opeDesc.trim().equals("")){
- //封装日志信息
- logger.info("开始封装日志信息。");
- OpeLog opeLog = new OpeLog();
- opeLog.setAdminId(admin.getId());
- opeLog.setCityCode(admin.getCityCode());
- opeLog.setOpeDesc("管理员id="+admin.getId()+"操作【"+opeDesc+"】");
- opeLog.setOpeTime(DateUtils.getNow());
- opeLog.setIsDelete("0");
- opeLogUtils.opeLogService.save(opeLog);
- logger.info("保存管理员操作日志成功,信息为【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null,admin.getCityCode(),opeDesc,DateUtils.getNow()});
- }else{
- logger.info("保存操作日志失败,参数不足【adminId:{},cityCode:{},opeDesc:{},opeTime:{}】",new Object[]{admin!=null?admin.getId():null, admin!=null?admin.getCityCode():null, opeDesc, DateUtils.getNow()});
- }
- } catch (Exception e) {
- logger.error("保存操作日志异常,异常信息为:" + e.getMessage(), e);
- }
- }
- }
静态工具类中使用注解注入service的更多相关文章
- 静态工具类中使用注解注入service实例
一般需要在一个工具类中使用@Autowired 注解注入一个service.但是由于工具类方法一般都写成static,所以直接注入就存在问题. 使用如下方式可以解决: /** * */ package ...
- spring 在静态工具类中使用注解注入bean
/** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件 ...
- SpringBoot 项目在静态工具类中注入 RedisTemplate
静态属性不能直接注入,可以通过其set方法进行注入.(注意生成的set方法需要去掉static). 在工具类里直接注入RedisTemplate,两种方法: (1)使用@Autowired priva ...
- 解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的s ...
- JAVA中SpringMVC获取bean方法,在工具类(utils)注入service
有时候我们会出现无法用注解 @Autowired 注入bean的情况,这个时候可以 通过contextLoader获取 WebApplicationContext ctx = ContextLoade ...
- 在静态工具类中需要注入mapper
在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" ini ...
- Spring 中将service 注入到普通的工具类中
记录一下,防止忘记. 要求每次生成一个和数据库不重复的组队码,于是就想在工具类中加入service注入 方法1(红框是注意的地方)
- Springboot在工具类(Util)中使用@Autowired注入Service
1. 使用@Component注解标记工具类MailUtil: 2. 使用@Autowired注入我们需要的bean: 3. 在工具类中编写init()函数,并使用@PostConstruct注解标记 ...
- 关于工具类静态方法调用@Autowired注入的service类问题
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...
随机推荐
- 纯js写验证码
<html> <head> <meta name="viewport" content="width=device-width" ...
- iOS - File Archive/UnArchive 文件压缩/解压
1.ZipArchive 方式 ZipArchive 只能对 zip 类文件进行压缩和解压缩 GitHub 网址:https://github.com/ZipArchive/ZipArchive Zi ...
- python仿微软记事本
# -*- coding=utf-8 -*- from tkinter import filedialog import tkinter as tk import tkinter.scrolledte ...
- PHPStorm怎么修改选中的背景颜色呢?
File -> Settings -> editor ->color&fonts->general->下拉框中的selection background-> ...
- 浅谈 MVP in Android
一.概述 对于MVP(Model View Presenter),大多数人都能说出一二:“MVC的演化版本”,“让Model和View完全解耦”等等.本篇博文仅是为了做下记录,提出一些自己的看法,和帮 ...
- Java 14 类型信息
14 类型信息 运行是识别对象和类的信息 两种方式RTTI 假定编译时已经知道所有的类型反射 运行时发现和使用类的信息 1 RTTI //多态 创建一个具体的对象(Circle Square Tria ...
- volley超时和重复请求问题
原文: Android Volley double post when have slow request I have a problem with Volley POST request on ...
- IE浏览器GET传参后台乱码
ie里面 get传递的字符串 为 gb2312 ,后台用的是utf-8类型 所以用 POST传递字符串到后端 否则进行js参数转码 encodeURI(""); 后端解码
- 激活MyEclipse 6.5方法-通过一段Java程序生成激活码
在MyEclipse中新建一个Java类,名为MyEclipseKeyGen,将下面的Java代码拷贝到MyEclipseKeyGen类中,先修改变量subscriber的值,然后运行程序即可获得Su ...
- 20160808_Shell书
1. http://item.jd.com/11075150.html 2.