1

I'm working on AOP for the first time. It's not working - Maybe I'm mixing up Spring AOP and AspectJ, I'm not sure.

dependency declaration in Gradle file

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    ...
    compile("org.aspectj:aspectjweaver:1.8.9")
    }
}

Configuration class

@Configuration
@EnableAspectJAutoProxy
public class AspectJConfiguration{

    @Bean
    public MailQueueServiceAspect mailQueueServiceAspect(){            
        return new MailQueueServiceAspect();
    }
}

Aspect Class

@Aspect
public class MailQueueServiceAspect {
    private final Logger log = LoggerFactory.getLogger(MailQueueServiceAspect.class);

    @Pointcut("execution(* com.biscom.bsend.core.SaveMailHelper.aopTest())")
    private void mailUpdatePointCut(){}

    @After("mailUpdatePointCut()")
    public void afterSampleCreation(JoinPoint joinPoint) {
        log.info("Mail Queue updated successfully.");
    }
}

Joint-Point holder class

public class SaveMailHelper extends AbstractHelper<SaveMailInput, SaveMailOutput> {
    @Inject
    private MailQueueService mailQueueService;

    private final Logger log = LoggerFactory.getLogger(SaveMailHelper.class);

    @Override
    protected void executeHelper(SaveMailInput input, SaveMailOutput output) throws AbstractException {
        MailQueue mailQueue = mailQueueService.get(input.getId(), new MailQueue());

        BlockExecutor e = new BlockExecutor();

        e.execute(input.getMailFrom().shouldSet(mailQueue.isNew(), mailQueue.getMailFrom()), () ->{
            mailQueue.setMailFrom(input.getMailFrom().get());
        });

        output.setEntity(mailQueue);
        aopTest();
    }

    public void aopTest(){
        log.info("AOPTest method called.");
    }
}

I also tried avoiding Configuration class by placing @Component annotation over Aspect class.

FARSiM
  • 71
  • 1
  • 7
  • 1
    Spring AOP will only work on method executions on Spring managed beans. Non of these apply to what you are doing. You are trying to intercept new instance construction on on a non spring managed bean. You will have to use load or compile time weaving to make that work (and it won't work with an execute point cut). – M. Deinum Feb 08 '17 at 07:05
  • @M.Deinum, `public void AOPTest()` is really a method, not a constructor. It is just badly named, starting with capital letters. But I agree that `SaveMailHelper` should be a `@Component` in order to make this work with Spring AOP. Probably it is also easier to make the aspect a component instead of instantiating it manually. – kriegaex Feb 08 '17 at 14:09
  • For some reason I read `new AOPTest()`. Non the less that will also not make it work as Spring AOP is proxy based on internal method calls don't pass through the proxy so no AOP will be applied. Only calls INTO the object pass through the proxy. – M. Deinum Feb 08 '17 at 14:32
  • Solved: OK, the bottom line of the problem is SaveMailHelper class is not a Spring Managed Bean as it's not annotated with Component/Service/... . I was missing the point, thanks @krieg for pointing the problem precisely, thanks Denim too. – FARSiM Feb 09 '17 at 06:50

0 Answers0