Skip to content

@Bean 方法非静态问题 #108

@fangzhengjin

Description

@fangzhengjin

MapstructAutoConfiguration 中的 springContextUtils 方法被标注为 @bean,并且返回了一个 BeanFactoryPostProcessor 类型的对象,但是该方法是非静态的。在这种情况下,Spring 会遇到一些问题,比如不能正确处理这个 bean 的生命周期注解(如 @Autowired, @resource@PostConstruct 等)。

原因在于 BeanFactoryPostProcessor 类型的 bean 是在 Spring 容器启动的早期阶段初始化的,而这些 bean 需要在容器完全初始化之前被处理。如果这个方法不是静态的,Spring 在创建 Configuration 类实例时,可能会遇到循环依赖或者生命周期管理问题,导致无法正常初始化这个 bean。

Spring 提示

@Bean method MapstructAutoConfiguration.springContextUtils is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions