/** * @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); } } }