Spring AOP 关于bean point cut designator 和方法?

Spring AOP on a bean point cut designator and method?

我目前正在尝试在一个 Spring 项目中使用面向方面的编程,其中切入点是一组 bean 名称和这些 bean 的特定方法的通配符表达式。

我一开始试过这个: @Before("bean(*Processor)") 但它适用于处理器 bean 的每个方法。

然后我尝试了: @Before("bean(*Processor) && execution(* execute(..))") 只是为了看看我是否可以在调用 execute 时将其应用于所有处理器 bean。

任何帮助使其仅适用于某些 bean 并且在其中使用某种方法的方法都很棒。

干杯,

AP.

I then tried: @Before("bean(*Processor) && execution(* execute(..))")

效果很好。如果它不适合你,也许你在其他地方有问题。这是我的 MCVE:

Spring 测试组件:

package de.scrum_master.spring.q70534907;

import org.springframework.stereotype.Service;

@Service
public class SomeService {
  public void doSomething() {}
  public void execute() {}
}
package de.scrum_master.spring.q70534907;

import org.springframework.stereotype.Service;

@Service
public class FirstProcessor {
  public void doSomething() {}
  public void execute() {}
}
package de.scrum_master.spring.q70534907;

import org.springframework.stereotype.Component;

@Component
public class SecondProcessor {
  public void doSomething() {}
  public void execute() {}
}

应用示例:

package de.scrum_master.spring.q70534907;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
public class DemoApplication {
  public static void main(String[] args) throws Throwable {
    try (ConfigurableApplicationContext appContext = SpringApplication.run(DemoApplication.class, args)) {
      doStuff(appContext);
    }
  }

  private static void doStuff(ConfigurableApplicationContext appContext) {
    appContext.getBean(SomeService.class).doSomething();
    appContext.getBean(SomeService.class).execute();
    appContext.getBean(FirstProcessor.class).doSomething();
    appContext.getBean(FirstProcessor.class).execute();
    appContext.getBean(SecondProcessor.class).doSomething();
    appContext.getBean(SecondProcessor.class).execute();
  }
}

看点:

package de.scrum_master.spring.q70534907;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
  @Before("bean(*Processor) && execution(* execute(..))")
  public void logBeanMethodsOfInterest(JoinPoint joinPoint) {
    System.out.println(joinPoint);
  }
}

控制台日志:

(...)
2021-12-31 10:41:39.422  INFO 16032 --- [           main] d.s.spring.q70534907.DemoApplication     : Started DemoApplication in 4.013 seconds (JVM running for 6.271)
execution(void de.scrum_master.spring.q70534907.FirstProcessor.execute())
execution(void de.scrum_master.spring.q70534907.SecondProcessor.execute())
2021-12-31 10:41:41.069  INFO 16032 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
(...)