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

Spring Context not loading with lazy-init from version 14.0.1 #511

Closed
michael4screen opened this issue Jul 8, 2024 · 4 comments · Fixed by #512
Closed

Spring Context not loading with lazy-init from version 14.0.1 #511

michael4screen opened this issue Jul 8, 2024 · 4 comments · Fixed by #512
Labels
bug released Issue has been released

Comments

@michael4screen
Copy link

Expected Behavior

Upgrade to version 14.0.1 is a patch version and does not include breaking changes.

Current Behavior

We are running spring boot integration tests with spring.main.lazy-initialization: true
Context cannot be loaded anymore:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.kagkarlsson.scheduler.boot.autoconfigure.DbSchedulerAutoConfiguration': Failed to instantiate [com.github.kagkarlsson.scheduler.boot.autoconfigure.DbSchedulerAutoConfiguration$$SpringCGLIB$$0]: No default constructor found

The root cause is this change, to not eagerly load the DbSchedulerStarter:
https://github.com/kagkarlsson/db-scheduler/pull/492/files


For bug reports

Context

  • DB-Scheduler Version : 14.0.2
  • Java Version : 21
  • Spring Boot (check for Yes) : [X]
  • Database and Version : Postgres 16
@kagkarlsson
Copy link
Owner

It looks like #492 might have to be reverted @NicklasWallgren
I can reproduce. Not familiar with how lazy-loading in Spring works exactly, so not sure what the fix might be.

Not sure why it would suddenly need to use the default constructor when it normally does not seem to.

@kagkarlsson kagkarlsson added the bug label Jul 8, 2024
@NicklasWallgren
Copy link
Contributor

NicklasWallgren commented Jul 8, 2024

@kagkarlsson The eagerDbSchedulerStarter needs to be declared as a static method, as it's defined in the same AutoConfiguration class as the DbSchedulerStarter bean, which has a args-constructor.

My bad - it works fine in our externalized configuration (since it's in a separate configuration class with a no-args constructor). I'll open a PR.

image

NOTE: Beans of this type will be instantiated very early in the spring application lifecycle so they should generally be declared static and not have any dependencies.

https://docs.spring.io/spring-boot/api/java/org/springframework/boot/LazyInitializationExcludeFilter.html#:~:text=NOTE%3A%20Beans%20of%20this%20type%20will%20be%20instantiated%20very%20early%20in%20the%20spring%20application%20lifecycle%20so%20they%20should%20generally%20be%20declared%20static%20and%20not%20have%20any%20dependencies.

@kagkarlsson
Copy link
Owner

Ok, thanks, will have a look 👍

kagkarlsson pushed a commit that referenced this issue Jul 23, 2024
…er as static (#512)

The `DbSchedulerAutoConfiguration#eagerDbSchedulerStarter` must be
declared as static to ensure it is available before the `initialization`
of `DbSchedulerAutoConfiguration`. Spring Boot will otherwise try to
instantiate the `DbSchedulerAutoConfiguration` class using a no-args
constructor.

> NOTE: Beans of this type will be instantiated very early in the spring
application lifecycle so they should generally be declared static and
not have any dependencies.


https://docs.spring.io/spring-boot/api/java/org/springframework/boot/LazyInitializationExcludeFilter.html#:~:text=NOTE%3A%20Beans%20of%20this%20type%20will%20be%20instantiated%20very%20early%20in%20the%20spring%20application%20lifecycle%20so%20they%20should%20generally%20be%20declared%20static%20and%20not%20have%20any%20dependencies.

Example - auto-configuration class with an args-constructor;

https://github.com/spring-projects/spring-boot/blob/ef303e74f7b92373e32d1cf95a121b37d377960a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/JmxEndpointAutoConfiguration.java#L116

Fixes #511

## Reminders
- [ ] Added/ran automated tests
- [ ] Update README and/or examples
- [X] Ran `mvn spotless:apply`

---
cc @kagkarlsson
@kagkarlsson kagkarlsson added the released Issue has been released label Jul 23, 2024
@kagkarlsson
Copy link
Owner

🎉 This issue has been resolved in v14.0.3 (Release Notes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug released Issue has been released
Projects
None yet
3 participants