Skip to content

Instantly share code, notes, and snippets.

@disc99
Created November 3, 2018 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disc99/3787633968f99901c2814dc4dcc25074 to your computer and use it in GitHub Desktop.
Save disc99/3787633968f99901c2814dc4dcc25074 to your computer and use it in GitHub Desktop.
よく使うAOP
import io.grpc.Status;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolationException;
import java.util.Arrays;
@Aspect
@Component
@Slf4j
public class ApplicationAdvice {
@Before("@annotation(org.springframework.cloud.stream.annotation.StreamListener)")
public void beforeStreamListener(JoinPoint joinPoint) {
log.info("{}#{}: {}",
joinPoint.getTarget().getClass(),
joinPoint.getSignature().getName(),
Arrays.toString(joinPoint.getArgs()));
}
@AfterThrowing(value="execution(* com.example.ui.grpc..*.*(..))", throwing="ex")
public void handle(RuntimeException ex) {
String message = ex.getMessage();
throw Status.INTERNAL
.withDescription(message == null ? ex.toString() : message)
.asRuntimeException();
}
@AfterThrowing(value="execution(* com.example.ui.rest..*.*(..))", throwing="ex")
public void handle(ConstraintViolationException ex) {
throw new BadRequestException(ex.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment