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.