auditinformationretentionaspect.java

来自「一些很有用的spring的书籍」· Java 代码 · 共 105 行

JAVA
105
字号
/*
package com.apress.springbook.chapter04.aspects;

import com.apress.springbook.chapter04.CurrentUserInformation;
import com.apress.springbook.chapter04.AuditInformationRetentionPolicy;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.JoinPoint;

@Aspect
public class AuditInformationRetentionAspect {

  private AuditInformationRetentionPolicy auditInformationRetentionPolicy;
  private CurrentUserInformation currentUserInformation;

  public void setAuditInformationRetentionPolicy(
    AuditInformationRetentionPolicy auditInformationRetentionPolicy
  ) {
    this.auditInformationRetentionPolicy = auditInformationRetentionPolicy;
  }

  public void setCurrentUserInformation
  (
    CurrentUserInformation currentUserInformation
  ) {
    this.currentUserInformation = currentUserInformation;
  }

  public void init() {
    if (this.auditInformationRetentionPolicy == null) {
      throw new IllegalStateException("AuditInformationRetentionPolicy " +
          "object is not set!");
    }
    if (this.currentUserInformation == null) {
      throw new IllegalStateException("CurrentUserInformation " +
          "object is not set!");
    }
  }

  @Before("com.apress.springbook.chapter04.aspects." +
          "SystemPointcutsAspect.auditableMethods()")
  public void retainMethodInvocationInformation(JoinPoint joinPoint) {
    String currentUser = this.currentUserInformation.getUsername();
    String methodDescription = joinPoint.getSignature().toLongString();
    Object[] arguments = joinPoint.getArgs();

    this.auditInformationRetentionPolicy.retainMethodInvocationInformation(
       currentUser, methodDescription, arguments);
  }
}
*/

package com.apress.springbook.chapter04.aspects;

import com.apress.springbook.chapter04.Audit;
import com.apress.springbook.chapter04.CurrentUserInformation;
import com.apress.springbook.chapter04.AuditInformationRetentionPolicy;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.JoinPoint;

@Aspect
public class AuditInformationRetentionAspect {
  private AuditInformationRetentionPolicy auditInformationRetentionPolicy;
  private CurrentUserInformation currentUserInformation;

  public void setAuditInformationRetentionPolicy(
       AuditInformationRetentionPolicy auditInformationRetentionPolicy) {
    this.auditInformationRetentionPolicy = auditInformationRetentionPolicy;
  }

  public void setCurrentUserInformation(
       CurrentUserInformation currentUserInformation) {
    this.currentUserInformation = currentUserInformation;
  }

  public void init() {
    if (this.auditInformationRetentionPolicy == null) {
      throw new IllegalStateException("AuditInformationRetentionPolicy " +
          "object is not set!");
    }

    if (this.currentUserInformation == null) {
      throw new IllegalStateException("CurrentUserInformation " +
          "object is not set!");
    }
  }

  @Before("com.apress.springbook.chapter04.aspects." +
          "SystemPointcutsAspect.auditableMethods(audit)")
  public void retainMethodInvocationInformation(JoinPoint joinPoint, Audit audit) {
    String currentUser = this.currentUserInformation.getUsername();
    String methodDescription = audit.value() + ":" + 
            joinPoint.getSignature().toLongString();
    Object[] arguments = joinPoint.getArgs();

    this.auditInformationRetentionPolicy.retainMethodInvocationInformation(
       currentUser, methodDescription, arguments);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?