spring 在靜態工具類中使用注解注入bean


/**
* @author: jerry
* @Email: 
* @Company:
* @Action: 日志處理工具類
* @DATE: 2016-9-19
*/

@Component//泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注
public class LogUtil {

@Autowired
//注意這里非靜態 private AdminLogService logService; private static LogUtil logUtil; @PostConstruct //@PostConstruct修飾的方法會在服務器加載Servle的時候運行,並且只會被服務器執行一次。PostConstruct在構造函數之后執行,init()方法之前執行 public void init() { logUtil = this; logUtil.logService = this.logService; } public AdminLogService getUserService() { return logService; } public void setUserService(AdminLogService userService) { this.logService = userService; } public static void addLog(LoginInfo loginInfo,String desc){ //添加日志 AdminLog log=new AdminLog(); log.setCreatetime(new Date()); log.setDescription(desc); try { log.setCuid(loginInfo.getUser().getId()); log.setCreateuser(loginInfo.getUser().getAccount()); logUtil.logService.insertLog(log); } catch (Exception e) { log.setCuid(0); log.setCreateuser("unknow");
//調用時通過類去調 logUtil.logService.insertLog(log); } } }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM