[TYPE] 类型的 Bean 'x' 不能被所有 BeanPostProcessor 处理 [英] Bean 'x' of type [TYPE] is not eligible for getting processed by all BeanPostProcessors

查看:48
本文介绍了[TYPE] 类型的 Bean 'x' 不能被所有 BeanPostProcessor 处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ResourceAspect 类:

//@Component
@Aspect
public class ResourceAspect {

    @Before("execution(public * *(..))")
    public void resourceAccessed() {
        System.out.println("Resource Accessed");
    }

}

这是我的 Application 类:

@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

每当我将 @Component 添加到 ResourceAspect 时,resourceAccessed() 都会执行,但它也会抛出一个 Exception Bean 'x' 类型 [TYPE] 不能被所有 BeanPostProcessor 处理.如果没有 @ComponentresourceAccessed() 不会执行.有什么想法吗?

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?

推荐答案

我的猜测是你的切入点 execution(* *(..))(基本上是说拦截世界")影响太多的组件,甚至是 Spring 内部的组件.只需将其限制为您真正想要应用方面的类或包,例如

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..*(..))

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(* *(..))

!within(my.problematic.package..*) && execution(* *(..))

顺便说一句,当使用 Spring AOP(而不是 AspectJ)时,您的方面当然需要是 @Component.

BTW, of course your aspect needs to be a @Component when using Spring AOP (not AspectJ).

这篇关于[TYPE] 类型的 Bean 'x' 不能被所有 BeanPostProcessor 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆