Issue
I have the following interface:
public interface Performance {
public void perform();
}
Implemented by the following class:
@Component
public class Woodstock implements Performance {
public void perform() {
System.out.println("Woodstock.perform()");
}
}
The aspect I want to apply is this one:
@Aspect
public class Audience {
@Pointcut("execution(* concert.Performance.perform(..)) "
+ "&& bean('woodstock')"
)
public void performance() {}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
The configuration file for the program is declaring the following beans:
@Bean
public Audience audience(){
return new Audience();
}
@Bean
public Performance woodstock(){
return new Woodstock();
}
And the test I'm running is this:
@Autowired
ApplicationContext context;
@Autowired
@Qualifier("woodstock")
Performance performance;
@Test
public void test(){
printBeanNames();
performance.perform();
}
Now, in the Audience class I specified in the annotation that I want to apply the aspect for the perform() method from the interface concert.Performance (execution(* concert.Performance.perform(..))
) and I am explicitly limiting the application of the aspect to the bean whose name is "woodstock" (&& bean('woodstock')
).
But when I run it I see that the aspect is not applied to the woodstock bean, although that bean is the one being used (it prints Woodstock.perform()
)
If instead of bean()
I use !bean()
then it does work, so it seems that it cannot consider that bean as the one named "woodstock".
Is there something wrong in the code above?
Solution
The special Spring AOP bean()
pointcut works like bean(myBean)
or bean(*Bean)
, there are no inline single or double quotes around the bean name according to the Spring AOP documentation (scroll down a little bit in this chapter).
Answered By - kriegaex