Skip to content

Commit 1498faa

Browse files
committed
Document auto-configuration packages and how to add to them
Closes gh-27549
1 parent eae95f8 commit 1498faa

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ You could take the JPA example from earlier and, assuming that `City` is now a M
146146
include::code:CityRepository[]
147147

148148
Repositories and documents are found through scanning.
149-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
149+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
150150
You can customize the locations to look for repositories and documents by using `@EnableMongoRepositories` and `@EntityScan` respectively.
151151

152152
TIP: For complete details of Spring Data MongoDB, including its rich object mapping technologies, see its {spring-data-mongodb}[reference documentation].
@@ -223,7 +223,7 @@ Spring Boot supports both classic and reactive Neo4j repositories, using the `Ne
223223
When Project Reactor is available on the classpath, the reactive style is also auto-configured.
224224

225225
Repositories and entities are found through scanning.
226-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
226+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
227227
You can customize the locations to look for repositories and entities by using `@EnableNeo4jRepositories` and `@EntityScan` respectively.
228228

229229
[NOTE]
@@ -355,7 +355,7 @@ In fact, both Spring Data JPA and Spring Data Elasticsearch share the same commo
355355
You could take the JPA example from earlier and, assuming that `City` is now an Elasticsearch `@Document` class rather than a JPA `@Entity`, it works in the same way.
356356

357357
Repositories and documents are found through scanning.
358-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
358+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
359359
You can customize the locations to look for repositories and documents by using `@EnableElasticsearchRepositories` and `@EntityScan` respectively.
360360

361361
TIP: For complete details of Spring Data Elasticsearch, see the {spring-data-elasticsearch-docs}[reference documentation].
@@ -445,7 +445,7 @@ Spring Data includes basic repository support for Cassandra.
445445
Currently, this is more limited than the JPA repositories discussed earlier and needs to annotate finder methods with `@Query`.
446446

447447
Repositories and entities are found through scanning.
448-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
448+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
449449
You can customize the locations to look for repositories and entities by using `@EnableCassandraRepositories` and `@EntityScan` respectively.
450450

451451
TIP: For complete details of Spring Data Cassandra, see the https://docs.spring.io/spring-data/cassandra/docs/[reference documentation].
@@ -500,7 +500,7 @@ To take more control, one or more `ClusterEnvironmentBuilderCustomizer` beans ca
500500
Spring Data includes repository support for Couchbase.
501501

502502
Repositories and documents are found through scanning.
503-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
503+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
504504
You can customize the locations to look for repositories and documents by using `@EnableCouchbaseRepositories` and `@EntityScan` respectively.
505505

506506
For complete details of Spring Data Couchbase, see the {spring-data-couchbase-docs}[reference documentation].
@@ -570,7 +570,7 @@ Make sure to flag your customized `ContextSource` as `@Primary` so that the auto
570570
Spring Data includes repository support for LDAP.
571571

572572
Repositories and documents are found through scanning.
573-
By default, the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) and all those below it are searched.
573+
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
574574
You can customize the locations to look for repositories and documents by using `@EnableLdapRepositories` and `@EntityScan` respectively.
575575

576576
For complete details of Spring Data LDAP, see the https://docs.spring.io/spring-data/ldap/docs/1.0.x/reference/html/[reference documentation].

spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/sql.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ You can follow the https://spring.io/guides/gs/accessing-data-jpa/["`Accessing D
195195
==== Entity Classes
196196
Traditionally, JPA "`Entity`" classes are specified in a `persistence.xml` file.
197197
With Spring Boot, this file is not necessary and "`Entity Scanning`" is used instead.
198-
By default, all packages below your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) are searched.
198+
By default the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
199199

200200
Any classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass` are considered.
201201
A typical entity class resembles the following example:
@@ -216,7 +216,7 @@ For example, a `CityRepository` interface might declare a `findAllByState(String
216216
For more complex queries, you can annotate your method with Spring Data's {spring-data-jpa-api}/repository/Query.html[`Query`] annotation.
217217

218218
Spring Data repositories usually extend from the {spring-data-commons-api}/repository/Repository.html[`Repository`] or {spring-data-commons-api}/repository/CrudRepository.html[`CrudRepository`] interfaces.
219-
If you use auto-configuration, repositories are searched from the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) down.
219+
If you use auto-configuration, the <<using#using.auto-configuration.packages,auto-configuration packages>> are searched for repositories.
220220

221221
TIP: You can customize the locations to look for repositories using `@EnableJpaRepositories`.
222222

@@ -506,7 +506,7 @@ For example, a `CityRepository` interface might declare a `findAllByState(String
506506
For more complex queries, you can annotate your method with Spring Data's {spring-data-r2dbc-api}/repository/Query.html[`Query`] annotation.
507507

508508
Spring Data repositories usually extend from the {spring-data-commons-api}/repository/Repository.html[`Repository`] or {spring-data-commons-api}/repository/CrudRepository.html[`CrudRepository`] interfaces.
509-
If you use auto-configuration, repositories are searched from the package containing your main configuration class (the one annotated with `@EnableAutoConfiguration` or `@SpringBootApplication`) down.
509+
If you use auto-configuration, the <<using#using.auto-configuration.packages,auto-configuration packages>> are searched for repositories.
510510

511511
The following example shows a typical Spring Data repository interface definition:
512512

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/data-access.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,23 @@ Note that each `configuration` sub namespace provides advanced settings based on
150150
[[howto.data-access.spring-data-repositories]]
151151
=== Use Spring Data Repositories
152152
Spring Data can create implementations of `@Repository` interfaces of various flavors.
153-
Spring Boot handles all of that for you, as long as those `@Repositories` are included in the same package (or a sub-package) of your `@EnableAutoConfiguration` class.
153+
Spring Boot handles all of that for you, as long as those `@Repositories` are included in one of the <<using#using.auto-configuration.packages,auto-configuration packages>>, typically the package (or a sub-package) of your main application class that is annotated with `@SpringBootApplication` or `@EnableAutoConfiguration`.
154154

155155
For many applications, all you need is to put the right Spring Data dependencies on your classpath.
156156
There is a `spring-boot-starter-data-jpa` for JPA, `spring-boot-starter-data-mongodb` for Mongodb, and various other starters for supported technologies.
157157
To get started, create some repository interfaces to handle your `@Entity` objects.
158158

159-
Spring Boot tries to guess the location of your `@Repository` definitions, based on the `@EnableAutoConfiguration` it finds.
160-
To get more control, use the `@EnableJpaRepositories` annotation (from Spring Data JPA).
159+
Spring Boot determines the location of your `@Repository` definitions by scanning the <<using#using.auto-configuration.packages,auto-configuration packages>>.
160+
For more control, use the `@Enable…Repositories` annotations from Spring Data.
161161

162162
For more about Spring Data, see the {spring-data}[Spring Data project page].
163163

164164

165165

166166
[[howto.data-access.separate-entity-definitions-from-spring-configuration]]
167167
=== Separate @Entity Definitions from Spring Configuration
168-
Spring Boot tries to guess the location of your `@Entity` definitions, based on the `@EnableAutoConfiguration` it finds.
169-
To get more control, you can use the `@EntityScan` annotation, as shown in the following example:
168+
Spring Boot determines the location of your `@Entity` definitions by scanning the <<using#using.auto-configuration.packages,auto-configuration packages>>.
169+
For more control, use the `@EntityScan` annotation, as shown in the following example:
170170

171171
include::code:MyApplication[]
172172

spring-boot-project/spring-boot-docs/src/docs/asciidoc/using/auto-configuration.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ TIP: You can define exclusions both at the annotation level and by using the pro
3535

3636
NOTE: Even though auto-configuration classes are `public`, the only aspect of the class that is considered public API is the name of the class which can be used for disabling the auto-configuration.
3737
The actual contents of those classes, such as nested configuration classes or bean methods are for internal use only and we do not recommend using those directly.
38+
39+
40+
[[using.auto-configuration.packages]]
41+
=== Auto-configuration Packages
42+
Auto-configuration packages are the packages that various auto-configured features look in by default when scanning for things such as entities and Spring Data repositories.
43+
The `@EnableAutoConfiguration` annotation (either directly or through its presence on `@SpringBootApplication`) determines the default auto-configuration package.
44+
Additional packages can be configured using the `@AutoConfigurationPackage` annotation.

0 commit comments

Comments
 (0)