Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spring aop

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Comment

spring aop

package dev.gayerie.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SupervisionAspect {

  @Around("@annotation(supervision)")
  public Object superviser(ProceedingJoinPoint joinPoint, Supervision supervision)
                      throws Throwable {
    long maxDuree = supervision.dureeMillis();
    long start = System.currentTimeMillis();
    try {
      return joinPoint.proceed(joinPoint.getArgs());
    } finally {
      long end = System.currentTimeMillis();
      long duree = end - start;
      if (duree > maxDuree) {
        System.out.printf("Attention l'appel à %s à durée %dms soit %dms de plus qu'attendu%n",
                          joinPoint.toShortString(), duree, duree - maxDuree);
      }
    }
  }

}
Comment

spring aop

package dev.gayerie;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

  @Before("execution(public * dev.gayerie.*Service.*(..))")
  public void log(JoinPoint joinPoint) {
    System.out.printf("Appel de %s avec %d paramètres%n",
                      joinPoint.toShortString(),
                      joinPoint.getArgs().length);
  }

}
Comment

spring aop

package dev.gayerie;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@Configuration
@ComponentScan
public class Application {

  public static void main(String[] args) throws InterruptedException {
    try (AnnotationConfigApplicationContext appCtx =
                  new AnnotationConfigApplicationContext(Application.class)) {
      // ...
    }
  }

}
Comment

spring aop

package dev.gayerie;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Supervision {

  int dureeMillis();

}
Comment

spring aop

package dev.gayerie;

import org.springframework.stereotype.Service;

@Service
public class BusinessService {

  @Supervision(dureeMillis = 5)
  public void doSomething() {
    System.out.println("réalise un traitement important pour l'application");
    // ...
  }

}
Comment

PREVIOUS NEXT
Code Example
Java :: how to check android version 9 above programatically 
Java :: get action command in java 
Java :: Example of ArrayDeque 
Java :: Algorithms - decision 
Java :: try catch still prints java 
Java :: error attribute fabattached not found 
Java :: ferrari class in java 
Java :: using condition for each loop 
Java :: change FS to hdfs java 
Java :: javafx.controls,javafx.fxml caused by: java.lang.classnotfoundexception: javafx.controls,javafx.fxml 
Java :: They say that they have to create an instance to their application. What does it mean? 
Java :: Java Numbers and Strings 
Java :: trivers json node as node type2 
Java :: add days to a date java 
Java :: spring converter in initbinder 
Java :: getter and setter in java 
Java :: java nested for loop 
Java :: query spring boot 
Java :: split each character in a string (java) 
Java :: android array to string 
Java :: final method java 
Java :: how to create a boolean list in java 
Java :: import class from package java 
Java :: for var i = 0 
Java :: $950 at 6% per annum for three years. 
Java :: Java StringBuilder filter same lines 
Sql :: stop mysql server mac 
Sql :: oracle all tables in schema 
Sql :: fetch first 5 rows in oracle sql developer 
Sql :: mysql dump all databases 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =