0
votes

I am using annotation based AOP in my Java Spring Application. There is no XML configuration at all (apart from log4j2.xml).

My first pointcut gets executed once as expected but every pointcut after that will get executed twice and I can't figure out why.

Here is my configuration class:

@Configuration @EnableAspectJAutoProxy
public class LoggingConfig
{
    @Bean public PreProcessLogs preProcessLogs(){
        return new PreProcessLogs();
    }
}

and the Aspect class:

@Aspect
public class PreProcessLogs
{
    @Before("execution(* <package>.preprocessor.services.DownloadService.addToDownloading(..)) " + "&& args(event)")
    public void LogFTPFile(JoinPoint joinPoint, WatchEvent<?> event) {
        Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getLogger(joinPoint.getSignature().getDeclaringTypeName());
        logger.log(Level.INFO, event.context().toString() + " has appeared in the FTP\n");
    }

    @AfterReturning("execution(void <package>.preprocessor.services.DownloadService.addToDownloaded(..)) " + "&& args(filePath)")
    public void logDownloadedFile(JoinPoint joinPoint, Path filePath) {
        //Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getLogger(joinPoint.getSignature().getDeclaringTypeName());
        //logger.log(Level.INFO, file.getFileName() + " has finished downloading");
        //System.out.println(joinPoint.getSourceLocation());
        System.out.println(filePath.getFileName() +" has been downloaded");
    }

    @After("execution(void <package>.preprocessor.services.FileTransferService.moveToStagingFolder(..)) " + "&& args(file)")
    public void logFileTransfer(JoinPoint jp, Path file) {
        System.out.println(file.getFileName() + " has been moved to the Staging Folder");
    }
}

So basically the @AfterReturning and @After Pointcuts get executed twice instead of once. The @Before annotation gets executed once as expected. I have tried @Before with the bottom two pointcuts but same effect. I have also tried to define a pointcut using @Pointcut but the same thing happens. I have also tried to specify the type where the (..)) is.

At the end of my tether trying to get this to work, please someone push me in the right direction.

Thanks.

1
No, they do not executed twice (unless someone calls the twice, too), you only think they do. But this cannot be analysed without the target code. So please provide an MCVE including aspect + application code incl. class and package names.kriegaex
I'm voting to close this question as it lacks the necessary minimal reproducible example to reproduce the issue.Nándor Előd Fekete

1 Answers

0
votes

Spring should not be processing aspect multiple times. Check if you are having @component or @service on top of the class. If yes, remove them and see. Also play around with @EnableJAutoproxy. It may be that two proxies are created, hence aspect is executing twice. Also, check if an annotated method is being called from another sibling method of the class. That might be also problematic.