Skip to content

GH-4103 - Use TypeScanner to scan for initial entities. - #5231

Open
masiljangajji wants to merge 3 commits into
spring-projects:mainfrom
masiljangajji:issue/4103-type-scanner
Open

GH-4103 - Use TypeScanner to scan for initial entities.#5231
masiljangajji wants to merge 3 commits into
spring-projects:mainfrom
masiljangajji:issue/4103-type-scanner

Conversation

@masiljangajji

@masiljangajji masiljangajji commented Aug 7, 2026

Copy link
Copy Markdown
  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

What

MongoConfigurationSupport.scanForEntities(String) scans entities using a manual ClassPathScanningCandidateComponentProvider loop instead of the store-agnostic TypeScanner abstraction that sibling modules (AbstractR2dbcConfiguration, JdbcConfiguration, CassandraEntityClassScanner) already use.

Why

#4103 tracks this: TypeScanner was introduced alongside Spring Data's AOT support specifically to avoid store-specific scanning implementations, and mongodb hadn't been migrated. Sibling modules already made the switch; mongodb was the one left behind.

Fix

Replaced the manual loop with TypeScanner.typeScanner(...).forTypesAnnotatedWith(Document.class).onClassNotFound(...).scanPackages(basePackage).collectAsSet(), matching the existing R2DBC/JDBC/Cassandra implementations — same shape as AbstractR2dbcConfiguration#scanForEntities.

scanForEntities(String) is a protected extension point that has been part of the API since 1.10, so the throws ClassNotFoundException declaration stays to keep source compatibility for overriding code, even though TypeScanner itself doesn't throw a checked exception. Class loading failures still fail fast, rethrown through onClassNotFound(...) — without it TypeScanner silently drops types it cannot load, which would be an observable behavior change. The exception type on that failure path does change from ClassNotFoundException to IllegalStateException, and the javadoc records that.

Added a characterization test (considersMetaAnnotatedTypesButNotInterfaces) covering directly annotated types, meta-annotated types, and annotated interfaces to confirm scanning semantics are unchanged; it passes against both the previous and the new implementation.

MappingMongoConverterParser (the XML namespace parser) has the same ClassPathScanningCandidateComponentProvider usage in two places; I left it out to keep this change scoped to a single call site.

This issue is assigned to @marcingrzejszczak, with no visible activity against it (comments, commits, PRs) in about 22 months. This change covers it.

Closes #4103

mp911de and others added 3 commits August 5, 2026 16:11
Replace the manual ClassPathScanningCandidateComponentProvider loop in
MongoConfigurationSupport.scanForEntities(String) with the store-agnostic
TypeScanner abstraction that was introduced along with the AOT support,
following AbstractR2dbcConfiguration, JdbcConfiguration and
CassandraEntityClassScanner.

Scanning semantics are retained. Both paths disable default filters and
match through AnnotationTypeFilter considering meta-annotations while
excluding interfaces. Added a characterization test covering directly
annotated types, meta-annotated types and annotated interfaces. It passes
against the previous implementation as well as the new one.

scanForEntities(String) is a protected extension point, so the
throws ClassNotFoundException declaration is retained to keep overriding
code source compatible. Class loading failures keep failing fast by
rethrowing through onClassNotFound(…), as TypeScanner would otherwise
silently drop types that cannot be loaded.

Closes: spring-projects#4103
Signed-off-by: masiljangajji <xmfpdlsj0508@gmail.com>
Signed-off-by: masiljangajji <xmfpdlsj0508@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use TypeScanner to scan for initial entities

3 participants