Bean 'x' of type [TYPE] is not eligible for getting processed by all BeanPostProcessors

ぐ巨炮叔叔 提交于 2021-02-17 06:48:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!