Skip to content

Custom converters cannot be used when creating endpoint-related beans due to eager initialization triggered by ServletEndpointRegistrar #20714

Closed
@WalkingGhost1975

Description

@WalkingGhost1975

This is related to the issues #6222 and #12148

It affects Spring Boot version 2.2.x

The behavior of configuration and value binding is still not completly consistent. The use of a custom Converter registered as bean or inside a conversionService bean only works in the following cases:

  • The configuration value will be bound to a configuration bean annotated with @ConfigurationProperties
  • The configuration value will be injected to bean via @Value annotation if the bean is created after complete refresh of the ApplicationContext.

It does not work, if the configuration value if injected into a bean with @Value if the bean is created during "refresh phase" of the ApplicationContext. This applies for example to all beans created during the initialization of the Tomcat server in a ServletWebServerApplicationContext

I provided a test project which contains multiple test cases showcasing the inconsistent behavior:
Test scenario 1: HealthIndicator bean created during refresh with configuration object => SUCCESS
Test scenario 2: HealthIndicator bean created during refresh with constructor injection via @Value => FAIL
Test scenario 3: @Service bean created after refresh with constructor injection via @Value => SUCCESS

spring-boot-converter-bug.zip

Please also note that the failure of scenario 2 can only be detected in an integration test if a "real" webEnvironment, e.g. RANDOM_PORT, is used. In a mocked web environment, i.e. without actually starting a Tomcat, the problem will not occur.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions