警告@Configuration类上的非静态ConfigurationClassPostProcessor声明 [英] Warn about non-static ConfigurationClassPostProcessor declarations on @Configuration classes

查看:358
本文介绍了警告@Configuration类上的非静态ConfigurationClassPostProcessor声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Spring 4.2.6.RELEASE. During initialization of my app I get such a warning:

[WARN] org.springframework.context.annotation.ConfigurationClassPostProcessor EnhanceConfigurationClasses:无法增强@Configuration bean 定义 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration $ TokenKeyEndpointRegistrar' 由于其单例实例创建得太早.典型的 原因是带有一个非静态@Bean方法 BeanDefinitionRegistryPostProcessor返回类型:考虑声明 诸如静态"之类的方法.

[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'.

我发现吉拉遇到了一个非常类似的问题:

I have found jira for a very similar problem:

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

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

but it is marked as closed and should be fixed in 4.2.6.RELEASE.

推荐答案

您的问题与spring进行的bean工厂后处理器初始化有关.

Your problem relates to bean factory post processor initialization by spring.

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

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

BeanFactoryPostProcessor interface is used for different enhancements before beans initialization in container.

与上述问题相关的最佳示例之一是PropertySourcesPlaceholderConfigurer.它是bean工厂后期处理程序,用于从单独的文件进行属性外部化.借助于@Value注释和语法"$ {property.name}"来解析Bean中的属性.
参考: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-placeholderconfigurer

One of the best examples related to the described problem is PropertySourcesPlaceholderConfigurer. It is bean factory post processor that is used for properties externalization from separate file. Properties in a bean are resolved with help of @Value annotation and syntax "${property.name}".
Ref: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-placeholderconfigurer

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

So, PropertySourcesPlaceholderConfigurer is the bean factory post processor. It should be initialized before any bean is created in the spring container, otherwise it can't do its job. It should change bean meta-data with correct values from properties files. That is why it should be declared static in configuration class:

@Configuration
public class AppConfig {

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

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

In java, static fields and methods are loaded the same time as particular class is loaded by the class loader. So, bean factory post processor declaration is available without need to create a bean of the particular holder class. Spring loads bean factory post processor from static method and later creates the bean of the configuration class.

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

Spring documentation emphasises this point in this paragraph: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-value-annotations

当前行为与票证中Juergen Hoeller所述的相同. Spring将在日志中打印一条警告,说明有关未声明使用静态修饰符的Bean工厂后处理器.

Current behavior is the same as decribed by Juergen Hoeller in the ticket. Spring will print in logs a warning about bean factory post processor declared without static modifier.

这篇关于警告@Configuration类上的非静态ConfigurationClassPostProcessor声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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