J'essaie d'enregistrer le nom de la classe du référentiel en utilisant AOP. Mais au moment de l'exécution, je reçois le nom de la classe proxy dans les logs car j'ai utilisé des données Spring dans mon projet, il y a donc une interface utilisée pour le référentiel qui est implémentée à la volée par Spring.
Voici le journal généré après l'exécution.
className - No of records returned : 38
2017-02-03 19:54:43,360 [INFO] className - Exiting method: getAlertPatterns]
2017-02-03 19:54:43,360 [INFO] className - No of records returned : 38
2017-02-03 19:54:43,360 [INFO] className - Exiting method: getAlertPatterns]
2017-02-03 19:54:43,363 [INFO] controllerClassName - Exiting method: getAlertPatterns]
2017-02-03 19:54:46,457 [INFO] controllerClassName - ActionTakenController***Entering method: getAlertActions]
2017-02-03 19:54:46,458 [INFO] className - ActionTakenServiceImpl****Entering method: getAlertActions]
2017-02-03 19:54:46,458 [INFO] className - $Proxy158****Entering method: getAlertActions]
2017-02-03 19:54:46,510 [INFO] repositoryClassName - java.util.ArrayList***
2017-02-03 19:54:46,511 [INFO] repositoryClassName - No of records returned : 2
2017-02-03 19:54:46,511 [INFO] repositoryClassName - Exiting method: getAlertActions]
2017-02-03 19:54:46,511 [INFO] className - No of records returned : 2
2017-02-03 19:54:46,511 [INFO] className - Exiting method: getAlertActions]
2017-02-03 19:54:46,511 [INFO] className - No of records returned : 2
2017-02-03 19:54:46,512 [INFO] className - Exiting method: getAlertActions]
2017-02-03 19:54:46,512 [INFO] controllerClassName - Exiting method: getAlertActions]
2017-02-03 19:54:50,488 [INFO] controllerClassName - InitialAnalysisController***Entering method: getAlertInitialAnalysis]
2017-02-03 19:54:50,495 [INFO] className - InitialAnalysisServiceImpl****Entering method: getAlertInitialAnalysis]
2017-02-03 19:54:50,505 [INFO] className - $Proxy144****Entering method: getAlertInitialAnalysis]
Le code utilisé pour configurer la classe aop est le suivant
private Logger logger = LogManager.getLogger("");
/** Pointcut for execution of methods on {@link Service} annotation */
@Pointcut("execution(public * (@org.springframework.web.bind.annotation.RestController com.nscorp.apps.wds.controller..*).*(..))")
public void controllerCalls() {
/** Pointcut for execution of methods on {@link Service} annotation */
}
/** Pointcut for execution of methods on {@link Service} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Service com.nscorp.apps.wds.services..*).*(..))")
public void serviceAnnotation() {
/** Pointcut for execution of methods on {@link Service} annotation */
}
/** Pointcut for execution of methods on {@link Repository} annotation */
@Pointcut("execution(public * (@org.springframework.stereotype.Repository com.nscorp.apps.wds.dao..*).*(..))")
public void repositoryAnnotation() {
/** Pointcut for execution of methods on {@link Repository} annotation */
}
/** Pointcut for execution of methods on {@link JpaRepository} interfaces */
@Pointcut("this (org.springframework.data.repository.Repository)")
public void jpaRepository() {
/** Pointcut for execution of methods on {@link JpaRepository} interfaces */
}
/** Pointcut for execution of combined methods serviceAnnotation() and controllerCalls() */
@Pointcut("serviceAnnotation() || jpaRepository() ")
public void performanceMonitor() {
/** Pointcut for execution of combined methods serviceAnnotation() and controllerCalls() */
}
/** Pointcut for execution of combined methods repositoryAnnotation() and jpaRepository()*/
@Pointcut(" jpaRepository() ")
public void jpaTransactionlogs() {
/** Pointcut for execution of combined methods repositoryAnnotation() and jpaRepository()*/
}
@Pointcut(" controllerCalls() ")
public void controllerParamCalls() {
//buisenessCalls
}
@Around("controllerParamCalls()")
public Object controllerClassName(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info(joinPoint.getTarget().getClass().getSimpleName()+"***" +"Entering method: " + joinPoint.getSignature().getName() + "]");
Object[] signatureArgs = joinPoint.getArgs();
for (Object signatureArg: signatureArgs) {
logger.info( "The arguments are ----" + signatureArg);
}
Object obj = joinPoint.proceed();
if (obj instanceof Collection) {
logger.info("No of records returned : " + ((Collection<?>) obj).size());
}
logger.info("Exiting method: " + joinPoint.getSignature().getName() + "]");
return obj;
}
@Around("performanceMonitor()")
public Object className(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info(joinPoint.getTarget().getClass().getSimpleName() +"****" +"Entering method: " + joinPoint.getSignature().getName() + "]");
Object obj = joinPoint.proceed();
if (obj instanceof Collection) {
logger.info("No of records returned : " + ((Collection) obj).size());
}
logger.info("Exiting method: " + joinPoint.getSignature().getName() + "]");
return obj;
}
L'exemple de classe de référentiel est
@Repository
public interface ActionTakenRepository extends CrudRepository<ActionTaken, BigInteger> {
@Query("Select actionTaken from ActionTaken actionTaken where actionTaken.status NOT IN (Select codeId from Codes where codeType='"+CommonConstants.INACTIVE+"') order by auditCreatedAt desc")
List<ActionTaken> getAlertActions();
}