做系統的日志管理, 需要對管理員的操作進行保存.
想法是使用自定義注解+AOP , 在Controller中的方法貼自定義注解是可以切入的,但是在ServiceImpl的方法中貼注解, 卻不切入.
有大牛說下,怎么解決嗎?
serviceImpl方法中使用了@Transactional 事務注解. 有人說是和事務aop沖突了,
下面是LogAspect類
@Component
@Aspect
public class ConsoleSystemLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleSystemLogAspect.class);
@Autowired
private IConsoleSystemLogService consoleSystemLogService;
@Pointcut("@annotation(com.inesv.root.common.annotation.RecordLog)")
public void logAspect(){}
@Before("logAspect()")
public void saveSystemLog(JoinPoint point) {
ConsoleSystemLog log = new ConsoleSystemLog();
// 獲取Request
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 設定ip
String ipAddress = request.getRemoteAddr();
log.setIpAddress(ipAddress);
// 設定當前登錄用戶的用戶名和id
ConsoleUser currentUser = (ConsoleUser) SecurityUtils.getSubject().getPrincipal();
log.setOpId(currentUser.getId());
log.setOpName(currentUser.getUsername());
// 設定時間
log.setOpTime(new Date());
// 設定方法
String simpleName = point.getSignature().getDeclaringType().getSimpleName();
String methodName = point.getSignature().getName();
String function = simpleName + "." + methodName;
log.setFunction(function);
// 設定引數
Object[] args = point.getArgs();
log.setParams(JSON.toJSONString(args));
LOGGER.info("操作日志:{}",JSON.toJSONString(log));
// 保存日志
consoleSystemLogService.save(log);
}
}
這是UserServiceImpl
@Service
public class ConsoleUserServiceImpl implements IConsoleUserService {
@Autowired
private ConsoleUserMapper consoleUserMapper;
@Autowired
private ConsoleUserRoleMapper consoleUserRoleMapper;
@Override
@RecordLog
@Transactional(rollbackFor = {Exception.class, RuntimeException.class},propagation = Propagation.NESTED)
public void delete(Long id) {
AssertUtils.isIdNull(id, "用戶id不能為空", Context.CODE_ARGS_ERROR);
// 邏輯洗掉用戶
consoleUserMapper.updateDeleteStatus(id);
}
}
uj5u.com熱心網友回復:
樓主,我的專案里也用到了自定義注解,不過是加在controller中的,剛剛試了下加在service方法中(同時還加了@Transactional),是可以的呀!日志成功被記錄下來了,樓主是不是沒在spring組態檔中加上
<aop:aspectj-autoproxy proxy-target-class="true" />
uj5u.com熱心網友回復:
我的專案是spring boot,不過大體的配置還是一樣的,下面是我的主要配置注解類
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogsDescribe {
/**
* 介面名稱
* @return
*/
String value() default "";
}
Aspect
@Aspect
@Component
public class LogsDescribeAspect {
private static final Logger logger = LoggerFactory.getLogger(LogsDescribeAspect.class);
@Resource(name="operationLogService")
private OperationLogService operationLogService;
@Pointcut("@annotation(com.huangbl.blog.config.LogsDescribe)")
private void cut() {
}
@Before("cut()")
public void before(JoinPoint joinPoint) {
logger.info("LogsDescribe...before");
}
@After("cut()")
public void after(JoinPoint joinPoint) {
logger.info("LogsDescribe...after");
}
}
service中使用注解的方法和你的一樣,代碼我就不發了
最后就是Aspect自動代理,在spring組態檔中加上(在spring boot中只需要在服務啟動類上加上@EnableAspectJAutoProxy)
<aop:aspectj-autoproxy proxy-target-class="true" />
uj5u.com熱心網友回復:
可能service層已經被很多切面切了,切面順序問題導致失效uj5u.com熱心網友回復:
可能是因為spring和springmvc是兩個容器造成的轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82233.html
標籤:Web 開發
上一篇:jsp 登錄與資料庫相連,不管登錄對錯,都只進入到一個頁面
下一篇:spring框架運行時的例外
