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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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.

0 commit comments

Comments
 (0)