gpt4 book ai didi

spring - 警告 @Configuration 类上的非静态 ConfigurationClassPostProcessor 声明

转载 作者:IT老高 更新时间:2023-10-28 13:57:58 30 4
gpt4 key购买 nike

我使用 Spring 4.2.6.RELEASE。在我的应用程序初始化期间,我收到这样的警告:

[WARN] org.springframework.context.annotation.ConfigurationClassPostProcessor enhanceConfigurationClasses: Cannot enhance @Configuration bean definition 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration$TokenKeyEndpointRegistrar' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.

我发现 jira 有一个非常相似的问题:

https://jira.spring.io/browse/SPR-14234

但它被标记为已关闭,应该在 4.2.6.RELEASE 中修复。

最佳答案

您的问题与 spring 的 bean factory 后处理器初始化有关。

引用文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-extension-factory-postprocessors

BeanFactoryPostProcessor 接口(interface)用于在容器中 bean 初始化之前进行不同的增强。

与所述问题相关的最佳示例之一是 PropertySourcesPlaceholderConfigurer。它是 bean factory 后处理器,用于从单独的文件中进行属性外部化。 bean 中的属性通过 @Value 注释和语法“${property.name}”解析。
引用:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-placeholderconfigurer

因此,PropertySourcesPlaceholderConfigurer 是 bean 工厂后处理器。它应该在 在 spring 容器中创建任何 bean 之前进行初始化,否则它无法完成它的工作。它应该使用属性文件中的正确值更改 bean 元数据。这就是为什么它应该在配置类中声明为静态的:

@Configuration
public class AppConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

在 java 中,静态字段和方法的加载与类加载器加载特定类的同时。因此,无需创建特定持有者类的 bean,即可使用 bean 工厂后处理器声明。 Spring从静态方法加载bean工厂后处理器,然后创建配置类的bean。

Spring 文档在本段中强调了这一点:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-value-annotations

当前行为与 Juergen Hoeller 在工单中描述的相同。 Spring 将在日志中打印关于声明没有静态修饰符的 bean 工厂后处理器的警告。

关于spring - 警告 @Configuration 类上的非静态 ConfigurationClassPostProcessor 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614313/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com