0

I have written code to configure aspects around the bean methods but the advice method doesn't get called. When I call OnlineFormSubmissionServices.processOnlineFormSubmission() function from my page controller, I expect to see the logs given in my MailingAspect class to be logged. But none of the advices are called. I have defined a @Aspect MailingAspect class

@Aspect
public class MailingAspect {
  @Before("execution(* com.modules.onlineforms.services.OnlineFormSubmissionServices.insertOnlineForm(..))")
  public void beforeMail(JoinPoint joinPoint) { // the pointcut signature
    log.error("beforeMail() is running!");
    log.error("hijacked : " + joinPoint.getSignature().getName());
    log.error("******");
  }

  @Before("execution(* com.modules.onlineforms.services.OnlineFormSubmissionServices.createEmail(..)) && args(yourString,..)")
  public void mailAfterReturning(JoinPoint joinPoint, String yourString) {
    // result should be in this format
    log.error("mailAfterReturning() is running!");
    log.error("hijacked : " + joinPoint.getSignature().getName());
    log.error("Method returned value is : " + yourString);
    log.error("******");
  }

  @After("execution(* com.modules.onlineforms.services.OnlineFormSubmissionServices.insertOnlineForm(..))")
  public void mailAfter(JoinPoint joinPoint) {
    System.out.println("mailAfter() is running!");
    System.out.println("hijacked : " + joinPoint.getSignature().getName());
    System.out.println("******");
  }
}

My bean OnlineFormSubmissionServices is as below

public class OnlineFormSubmissionServices  {
  public String processOnlineFormSubmission(HttpServletRequest request, int publicationId){
    String strReturnVal = "";
    strReturnVal = insertOnlineForm(request, publicationId);
    if(strReturnVal.contains("success~")){
      createEmail(strReturnVal); // this is an Aspects method
    }
    return strReturnVal;
  }

  // ...
  public String insertOnlineForm(param1, param2){
  }
  public String createEmail(String sParam){
    log.error("createEmail "+sParam);
    return sParam;
  }
}

And my bean configuration is as below

@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
  @Bean
  public OnlineFormSubmissionServices onlineFormSubmissionServices() {
    OnlineFormSubmissionServices onlineFormSubmissionServices = new OnlineFormSubmissionServices();
    log.error("Inside  OnlineFormSubmissionServices initialization ");
    return onlineFormSubmissionServices;
  }

  @Bean
  //Aspect
  public MailingAspect mailingAspect() {
    MailingAspect mailingAspect = new MailingAspect();
    log.error("Inside MailingAspect initialization ");
    return mailingAspect;
  }
}

Can someone tell me what i am doing wrong

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Rache
  • 217
  • 1
  • 7
  • 20
  • The question is unclear. "Aspect doesn't get called" hardly qualifies as a problem description. So I can only guess about what you might be doing wrong. It could be all sorts of things: configuration, pointcuts, not declaring beans or aspects as Spring components etc. I am going to take a shot anyway. Stand by for my answer. – kriegaex Mar 10 '18 at 10:58
  • I will expand my question. – Rache Mar 10 '18 at 12:40
  • Or you can take a shot at my educated guesses first and report back. – kriegaex Mar 10 '18 at 13:05

1 Answers1

0

I have to guess what you actually want to ask:

  • If you want to know why method processOnlineFormSubmission(..) - the only method you are sharing here in your question - is not intercepted, the answer is simple: because your aspect has three pointcuts intercepting other methods.

  • If you are wondering why methods insertOnlineForm(..) and createEmail(..) are not being intercepted when called internally (not from another bean or component), it is simply because of the well documented fact that Spring AOP is proxy-based and thus cannot intercept self-invocation. This question has been asked about 100x here already. Obviously nobody bothers to RTFM anymore nowadays. Please also note that for Spring AOP to work the intercepted methods must not be private either. And BTW, AspectJ has no such limitations. You can find a description about how to configure Spring to use it here.

kriegaex
  • 63,017
  • 15
  • 111
  • 202