问题
I have a ResourceAspect
class:
//@Component
@Aspect
public class ResourceAspect {
@Before("execution(public * *(..))")
public void resourceAccessed() {
System.out.println("Resource Accessed");
}
}
Here is my Application
class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.run(args);
}
}
The dependencies that are being used inside the project are:
- spring-boot-starter
- spring-boot-configuration-processor
- spring-boot-starter-web
- spring-boot-starter-test
- spring-boot-devtools
- spring-boot-starter-security
- spring-boot-starter-aop
Whenever I add @Component
to the ResourceAspect
, the resourceAccessed()
executes but it also throws an Exception Bean 'x' of type [TYPE] is not eligible for getting processed by all BeanPostProcessors
. Without @Component
, resourceAccessed()
does not execute. Any ideas?
回答1:
My guess is that your pointcut execution(* *(..))
(which basically says "intercept the world") affects too many components, even Spring-internal ones. Just limit it to the classes or packages you really want to apply your aspect to, e.g.
execution(* my.package.of.interest..*(..))
or
within(my.package.of.interest..*) && execution(* *(..))
You could also exclude the ones you don't need woven if that is easier to specify:
!within(my.problematic.ClassName) && execution(* *(..))
or
!within(my.problematic.package..*) && execution(* *(..))
BTW, of course your aspect needs to be a @Component
when using Spring AOP (not AspectJ).
来源:https://stackoverflow.com/questions/57629309/bean-x-of-type-type-is-not-eligible-for-getting-processed-by-all-beanpostpro