Skip to content

Doc: Lifecycle (not SmartLifecycle) beans not started at refresh() time [SPR-12855] #17453

Closed
@spring-projects-issues

Description

@spring-projects-issues

Edward Sargisson opened SPR-12855 and commented

The docs say that a top-level bean may implement Lifecycle and have its start method called at startup.

Using such a bean with XmlWebApplicationContext does not have its start method called. Only a bean that implements SmartLifecycle does.

The defects appears to be that FrameworkServlet.configureAndRefreshWebApplicationContext only calls refresh on the web application context. It only ever calls refresh(). This means that the DefaultLifecycleProcessor ends up calling startBeans(autoStartupOnly=true).

In startBeans the following check means that only SmartLifecycle beans are added to the phases collection (and then started).
if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup()))

I found this in 4.0.5 but a check of the source on github suggests it's still there.

If there are other code paths that result in DefaultLifecycleProcessor.start() being called I couldn't find them.

Workaround

Implement SmartLifecycle instead of Lifecycle.


Affects: 3.2.13, 4.0.5

Backported to: 3.2.14

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions