Spring AspectJ通知方法未执行[重复]

gojuced7  于 2023-03-16  发布在  Spring
关注(0)|答案(1)|浏览(98)

此问题在此处已有答案

Why does self-invocation not work for Spring proxies (e.g. with AOP)?(1个答案)
两年前关闭了。
我已经编写了代码来配置bean方法周围的方面,但是通知方法没有被调用。当我从页面控制器调用OnlineFormSubmissionServices.processOnlineFormSubmission()函数时,我希望看到MailingAspect类中给出的日志被记录。但是没有一个通知被调用。我定义了一个@Aspect MailingAspect类

@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("******");
  }
}

我的bean OnlineFormSubmissionServices如下

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;
  }
}

我的bean配置如下所示

@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;
  }
}

有人能告诉我我做错了什么吗

cig3rfwq

cig3rfwq1#

我得猜猜你到底想问什么:

  • 如果你想知道为什么processOnlineFormSubmission(..)方法--你在这里分享的唯一方法--没有被拦截,答案很简单:因为你的方面有三个截取其他方法的切入点。
  • 如果您想知道为什么在内部调用方法insertOnlineForm(..)createEmail(..)时没有被拦截(而不是来自另一个bean或组件),这仅仅是因为Spring AOP是基于代理的,因此不能拦截自调用。这个问题在这里已经被问了100次了。显然现在没有人再为RTFM费心了。还请注意,要使Spring AOP工作,被拦截的方法也不能是私有的。顺便说一句,AspectJ没有这样的限制。您可以在这里找到关于如何配置Spring以使用它的描述。

相关问题