Skip to content

Commit 6505e03

Browse files
committed
Polish "Add auto-configuration for Spring Data Envers"
See gh-22610
1 parent 91da8c9 commit 6505e03

File tree

15 files changed

+130
-19
lines changed

15 files changed

+130
-19
lines changed

spring-boot-project/spring-boot-autoconfigure/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ dependencies {
112112
optional("org.springframework:spring-webmvc")
113113
optional("org.springframework.batch:spring-batch-core")
114114
optional("org.springframework.data:spring-data-couchbase")
115-
optional("org.springframework.data:spring-data-jpa")
116115
optional("org.springframework.data:spring-data-envers")
116+
optional("org.springframework.data:spring-data-jpa")
117117
optional("org.springframework.data:spring-data-rest-webmvc")
118118
optional("org.springframework.data:spring-data-cassandra")
119119
optional("org.springframework.data:spring-data-elasticsearch") {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/EnversRevisionRepositoriesRegistrar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,26 @@
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
3029
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
30+
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration.JpaRepositoriesImportSelector;
3131
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer;
3232
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3333
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3434
import org.springframework.context.annotation.Bean;
3535
import org.springframework.context.annotation.Conditional;
3636
import org.springframework.context.annotation.Configuration;
3737
import org.springframework.context.annotation.Import;
38+
import org.springframework.context.annotation.ImportSelector;
3839
import org.springframework.core.task.AsyncTaskExecutor;
40+
import org.springframework.core.type.AnnotationMetadata;
3941
import org.springframework.data.envers.repository.config.EnableEnversRepositories;
4042
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
4143
import org.springframework.data.jpa.repository.JpaRepository;
4244
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4345
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
4446
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
4547
import org.springframework.data.repository.history.RevisionRepository;
48+
import org.springframework.util.ClassUtils;
4649

4750
/**
4851
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JPA Repositories.
@@ -74,6 +77,7 @@
7477
@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class, JpaRepositoryConfigExtension.class })
7578
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true",
7679
matchIfMissing = true)
80+
@Import(JpaRepositoriesImportSelector.class)
7781
@AutoConfigureAfter({ HibernateJpaAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
7882
public class JpaRepositoriesAutoConfiguration {
7983

@@ -115,17 +119,21 @@ static class LazyBootstrapMode {
115119

116120
}
117121

118-
@Configuration(proxyBeanMethods = false)
119-
@ConditionalOnClass(EnableEnversRepositories.class)
120-
@Import(EnversRevisionRepositoriesRegistrar.class)
121-
public static class EnversRevisionRepositoriesRegistrarConfiguration {
122+
static class JpaRepositoriesImportSelector implements ImportSelector {
122123

123-
}
124+
private static final boolean ENVERS_AVAILABLE = ClassUtils.isPresent(
125+
"org.springframework.data.envers.repository.config.EnableEnversRepositories",
126+
JpaRepositoriesImportSelector.class.getClassLoader());
127+
128+
@Override
129+
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
130+
return new String[] { determineImport() };
131+
}
124132

125-
@Configuration(proxyBeanMethods = false)
126-
@ConditionalOnMissingClass("org.springframework.data.envers.repository.config.EnableEnversRepositories")
127-
@Import(JpaRepositoriesRegistrar.class)
128-
public static class JpaRepositoriesRegistrarConfiguration {
133+
private String determineImport() {
134+
return ENVERS_AVAILABLE ? EnversRevisionRepositoriesRegistrar.class.getName()
135+
: JpaRepositoriesRegistrar.class.getName();
136+
}
129137

130138
}
131139

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/AbstractJpaRepositoriesAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/EnversRevisionRepositoriesAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/city/City.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/city/CityRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/country/Country.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/country/CountryRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-docs/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ dependencies {
100100
implementation("org.springframework.data:spring-data-cassandra")
101101
implementation("org.springframework.data:spring-data-couchbase")
102102
implementation("org.springframework.data:spring-data-elasticsearch")
103+
implementation("org.springframework.data:spring-data-envers")
103104
implementation("org.springframework.data:spring-data-jpa")
104105
implementation("org.springframework.data:spring-data-ldap")
105106
implementation("org.springframework.data:spring-data-mongodb")
@@ -226,6 +227,7 @@ tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
226227
"spring-boot-version": project.version,
227228
"spring-data-commons-version": versionConstraints["org.springframework.data:spring-data-commons"],
228229
"spring-data-couchbase-version": versionConstraints["org.springframework.data:spring-data-couchbase"],
230+
"spring-data-envers-version": versionConstraints["org.springframework.data:spring-data-envers"],
229231
"spring-data-jdbc-version": versionConstraints["org.springframework.data:spring-data-jdbc"],
230232
"spring-data-jpa-version": versionConstraints["org.springframework.data:spring-data-jpa"],
231233
"spring-data-mongodb-version": versionConstraints["org.springframework.data:spring-data-mongodb"],

spring-boot-project/spring-boot-docs/src/docs/asciidoc/attributes.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
:spring-data-couchbase-docs: https://docs.spring.io/spring-data/couchbase/docs/{spring-data-couchbase-version}/reference/html/
6161
:spring-data-elasticsearch: https://spring.io/projects/spring-data-elasticsearch
6262
:spring-data-elasticsearch-docs: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/
63+
:spring-data-envers: https://spring.io/projects/spring-data-envers
64+
:spring-data-envers-doc: https://docs.spring.io/spring-data/envers/docs/{spring-data-envers-version}/reference/html/
6365
:spring-data-gemfire: https://spring.io/projects/spring-data-gemfire
6466
:spring-data-geode: https://spring.io/projects/spring-data-geode
6567
:spring-data-jpa: https://spring.io/projects/spring-data-jpa

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

+15
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ For complete details, see the {spring-data-jpa-docs}[Spring Data JPA reference d
247247

248248

249249

250+
[[features.sql.jpa-and-spring-data.envers-repositories]]
251+
==== Spring Data Envers Repositories
252+
If {spring-data-envers}[Spring Data Envers] is available, JPA repositories are auto-configured to support typical Envers queries.
253+
254+
To use Spring Data Envers, make sure your repository extends from `RevisionRepository` as show in the following example:
255+
256+
[source,java,indent=0,subs="verbatim"]
257+
----
258+
include::{docs-java}/features/sql/jpaandspringdata/repositories/CountryRepository.java[]
259+
----
260+
261+
NOTE: For more details, check the {spring-data-envers-doc}[Spring Data Envers reference documentation].
262+
263+
264+
250265
[[features.sql.jpa-and-spring-data.creating-and-dropping]]
251266
==== Creating and Dropping JPA Databases
252267
By default, JPA databases are automatically created *only* if you use an embedded database (H2, HSQL, or Derby).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.sql.jpaandspringdata.entityclasses;
18+
19+
import java.io.Serializable;
20+
21+
import javax.persistence.Column;
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
26+
import org.hibernate.envers.Audited;
27+
28+
@Entity
29+
public class Country implements Serializable {
30+
31+
@Id
32+
@GeneratedValue
33+
private Long id;
34+
35+
@Audited
36+
@Column(nullable = false)
37+
private String name;
38+
39+
public Long getId() {
40+
return this.id;
41+
}
42+
43+
public void setId(Long id) {
44+
this.id = id;
45+
}
46+
47+
public String getName() {
48+
return this.name;
49+
}
50+
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.sql.jpaandspringdata.repositories;
18+
19+
import org.springframework.boot.docs.features.sql.jpaandspringdata.entityclasses.Country;
20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.Pageable;
22+
import org.springframework.data.repository.Repository;
23+
import org.springframework.data.repository.history.RevisionRepository;
24+
25+
public interface CountryRepository extends RevisionRepository<Country, Long, Integer>, Repository<Country, Long> {
26+
27+
Page<Country> findAll(Pageable pageable);
28+
29+
}

0 commit comments

Comments
 (0)