静态工具类中使用注解注入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 ...
随机推荐
- zoj 1648 判断线段是否相交
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648 Circuit Board Time Limit: 2 Second ...
- ubuntu的dns设置
ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s
- HDU 1394
单点,利用线段树解题,看到数据大小一定要敏感,说不定就是暗藏的解题思路 #include <stdio.h> #define lson l,mid,id<<1 #define ...
- [转载] tmux 使用指南
原文: https://danielmiessler.com/study/tmux/ tmux的用法和screen类似, 比screen好用一些, 不过需要单独安装
- jquery.pagination.js分页
参数说明 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每页显示的条目数 ...
- Java中List Set Map 是否有序等总结
1.Collection List Set Map 区别记忆 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文 ...
- (四)802.1Q VLAN
- 使用火狐的restclient发送http接口post及get请求
1.在firefox安装restclient插件,具体参照http://jingyan.baidu.com/article/1876c8529b07e3890b137623.html: —.发送pos ...
- ffmpeg,X264编码结果I帧QP比P帧还大
enc_ctx->profile =FF_PROFILE_H264_MAIN ; enc_ctx->time_base.den = 24; enc_ctx->time_base.nu ...
- XML Attributes(XML属性)
XML Attributes(XML属性) android:autoLink 是否自动链接网址或邮箱地址: android:autoText 自动检测错误: android:bufferType ...