spring 静态注入
来源:互联网 发布:电脑淘宝怎样联系卖家 编辑:程序博客网 时间:2024/06/10 00:49
一般需要在一个工具类中使用@Autowired 注解注入一个service。但是由于工具类方法一般都写成static,所以直接注入就存在问题。
使用如下方式可以解决:
- /**
- *
- */
- 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);
- }
- }
- }
阅读全文
0 0
- Spring的静态注入
- Spring静态变量注入
- Spring静态变量注入
- spring静态变量注入
- spring 注入静态属性
- Spring静态注入
- spring静态注入
- Spring不支持静态注入
- Spring注入静态变量
- Spring静态注入
- spring静态类注入
- Spring静态变量注入
- spring 静态注入
- Spring 注入静态变量 解决方案
- Spring 注入静态变量 解决方案
- Spring 注入静态变量 解决方案
- Spring如何注入静态变量
- Spring 注入静态变量 解决方案
- BankCardUtils(根据银行卡号 获取 银行卡类型、银行名称和银行编码 自动格式化银行卡号、手机号、身份证号输入的工具类)
- 第三周 【项目3
- linux命令学习(1):ls命令
- Ngnix的理解
- AndroidStudio导入其他来源项目容易出现的问题
- spring 静态注入
- git pull出现There is no tracking information for the current branch
- Eclipse开发的相关问题总结
- phpstudy安装mongodb扩展
- 源码剖析之poll
- Python装饰器最经典教程,以最易理解的方式教会你
- C/C++中const关键字详解
- 用java操作hdfs
- 如何使用logisim的真值表自动生成电路?【木森小教程】