Skip to content

Change in bean lifecycle due to DefaultBinderFactory causes unexpected startup of LifeCycle beans #3155

@francisco-bru

Description

@francisco-bru

After the change introduced in f259c55, we have detected a change in behavior during the startup of Spring Boot applications.

Prior to this change, when refreshContext was executed in SpringApplication, it eventually called the onRefresh method in DefaultLifecycleProcessor. This method only forces the startup of SmartLifeCycle beans where isAutoStartup() returns true, ignoring the rest.
LifeCycle beans that are not SmartLifeCycle were not started at this point.

As of the mentioned commit, DefaultBinderFactory becomes a SmartLifeCycle bean, triggering the startup of other ConfigurationApplicationContext instances, and in this second call, the start() method is invoked instead of onRefresh(). This results in startBeans(false) being called in DefaultLifecycleProcessor, which also forces the startup of LifeCycle beans that are not SmartLifeCycle.

A concrete example: the integrationHeaderChannelRegistry bean (spring-integration-core), which previously was instantiated but never started and did not schedule its 'reaper' task. Now it does start, launches its 'reaper' task, and causes issues such as the one reported in spring-projects/spring-integration#10547.

Describe the problem
This change in behavior may cause unexpected side effects in LifeCycle beans that are not SmartLifeCycle, such as undesired scheduled tasks.

References

Steps to reproduce

  1. Start a Spring Boot application with spring-cloud-stream version (4.3.X).
  2. Verify that integrationHeaderChannelRegistry starts and launches its "reaper" task.
    • This can be verified by enabling TRACE log for org.springframework.integration.channel.DefaultHeaderChannelRegistry.
  3. Start the same Spring Boot application with spring-cloud-stream version (4.2.X).
  4. Verify that integrationHeaderChannelRegistry does not start and does not launch its "reaper" task.
    • No traces will be shown for the log org.springframework.integration.channel.DefaultHeaderChannelRegistry: TRACE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions