Skip to content

Conversation

@Jarvx
Copy link

@Jarvx Jarvx commented Oct 21, 2025

Issue: #11758

Bug description

The bug occurs because replacement beans (@Replaces) are only discovered among the candidate collection being considered, not across the global registry. As a result, replacements declared elsewhere in the registry (e.g., a non-@context replacement for a @context bean) can be missed, and already-cached singleton instances for the replaced bean can remain in singleton caches, leading to nondeterministic behavior.

Solution / fix

  1. Expand replacement detection in DefaultBeanContext.filterReplacedBeans to scan the global bean registry (beanDefinitionsClasses) for definitions annotated with @Replaces, in addition to any replacement candidates already present. For scanning we prefer checking the BeanDefinitionReference metadata first and only load the full BeanDefinition via the producer if necessary. This will find replacements declared outside the immediate candidate collection.

  2. When we identify that a bean definition is being replaced, invalidate the relevant caches to prevent returning an already-cached / eagerly-instantiated replaced instance. DefaultBeanContext already has purgeCacheForBeanType(Class) which removes candidate caches and cached bean registrations for that type; we call that. In addition, we add a targeted removal API in SingletonScope to remove any singleton registration tied to the replaced BeanDefinitionIdentity so the singleton scope will not return the old instance.

These changes are minimal and targeted: we avoid global cache clears and invalidate only the entries affected by the replaced bean definition.

Steps to reproduce

./gradlew :test-suite-kotlin-ksp:test

Jarvx added 2 commits October 21, 2025 20:42
…the reference metadata first and only loading definitions if a @Replaces stereotype is present on the reference.
@Jarvx
Copy link
Author

Jarvx commented Oct 24, 2025

I'll close this pr first because the CI fails.

@Jarvx Jarvx closed this Oct 24, 2025
@Jarvx Jarvx changed the title Fix 11758: Ensure @Replaces beans are detected globally [Gen AI] Fix 11758: Ensure @Replaces beans are detected globally Oct 24, 2025
@Jarvx Jarvx reopened this Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant