Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnnotationConfigWebApplicationContext should propagate ApplicationStartup to BeanFactory #32747

Closed
gavlyukovskiy opened this issue May 1, 2024 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@gavlyukovskiy
Copy link

gavlyukovskiy commented May 1, 2024

Affects: 5.3.x, 6.0.x, 6.1.x, 6.2.x


When setting ApplicationStartup on AnnotationConfigWebApplicationContext (or to be precise any AbstractApplicationContext that isn't also GenericApplicationContext) it's not getting propagated to BeanFactory after it was created:

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
FlightRecorderApplicationStartup applicationStartup = new FlightRecorderApplicationStartup();
context.setApplicationStartup(applicationStartup);
context.refresh();
System.out.println(context.getBeanFactory().getApplicationStartup() == applicationStartup); // false

and as a result no bean events are recorded.

It's also not possible to set applicationStartup on the bean factory because it's not created yet, so that results in

BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

As a workaround we can extend the AnnotationConfigWebApplicationContext and either override createBeanFactory

@Override
protected DefaultListableBeanFactory createBeanFactory() {
    DefaultListableBeanFactory beanFactory = super.createBeanFactory();
    beanFactory.setApplicationStartup(getApplicationStartup());
    return beanFactory;
}

or override customizeBeanFactory that already propagates other context settings to the BeanFactory

@Override
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
    super.customizeBeanFactory(beanFactory);
    beanFactory.setApplicationStartup(getApplicationStartup());
}

I think that should be done by default from the framework.

P.S. it doesn't reproduce with spring-boot because it uses different (similar?) context - AnnotationConfigServletWebApplicationContext

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 1, 2024
@jhoeller jhoeller added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 1, 2024
@jhoeller jhoeller self-assigned this May 1, 2024
@jhoeller jhoeller added this to the 6.1.7 milestone May 1, 2024
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x labels May 1, 2024
jhoeller added a commit that referenced this issue May 1, 2024

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
Closes gh-32747

(cherry picked from commit 25cedcf)
jhoeller added a commit that referenced this issue May 1, 2024

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
Closes gh-32747

(cherry picked from commit 25cedcf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants