Skip to content

Commit fbbfe6a

Browse files
committed
Merge pull request #22610 from scordio
* pr/22610: Polish "Add auto-configuration for Spring Data Envers" Add auto-configuration for Spring Data Envers Closes gh-22610
2 parents de350a0 + 6505e03 commit fbbfe6a

File tree

13 files changed

+494
-149
lines changed

13 files changed

+494
-149
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ 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-envers")
115116
optional("org.springframework.data:spring-data-jpa")
116117
optional("org.springframework.data:spring-data-rest-webmvc")
117118
optional("org.springframework.data:spring-data-cassandra")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.autoconfigure.data.jpa;
18+
19+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
20+
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
21+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
22+
23+
/**
24+
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Envers
25+
* Repositories.
26+
*
27+
* @author Stefano Cordio
28+
*/
29+
class EnversRevisionRepositoriesRegistrar extends JpaRepositoriesRegistrar {
30+
31+
@Override
32+
protected Class<?> getConfiguration() {
33+
return EnableJpaRepositoriesConfiguration.class;
34+
}
35+
36+
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
37+
private static class EnableJpaRepositoriesConfiguration {
38+
39+
}
40+
41+
}

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

+32-1
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,25 @@
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
30+
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration.JpaRepositoriesImportSelector;
3031
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer;
3132
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3233
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Conditional;
3536
import org.springframework.context.annotation.Configuration;
3637
import org.springframework.context.annotation.Import;
38+
import org.springframework.context.annotation.ImportSelector;
3739
import org.springframework.core.task.AsyncTaskExecutor;
40+
import org.springframework.core.type.AnnotationMetadata;
41+
import org.springframework.data.envers.repository.config.EnableEnversRepositories;
42+
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
3843
import org.springframework.data.jpa.repository.JpaRepository;
3944
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4045
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
4146
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
47+
import org.springframework.data.repository.history.RevisionRepository;
48+
import org.springframework.util.ClassUtils;
4249

4350
/**
4451
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JPA Repositories.
@@ -50,11 +57,17 @@
5057
* Once in effect, the auto-configuration is the equivalent of enabling JPA repositories
5158
* using the {@link EnableJpaRepositories @EnableJpaRepositories} annotation.
5259
* <p>
60+
* In case {@link EnableEnversRepositories} is on the classpath,
61+
* {@link EnversRevisionRepositoryFactoryBean} is used instead of
62+
* {@link JpaRepositoryFactoryBean} to support {@link RevisionRepository} with Hibernate
63+
* Envers.
64+
* <p>
5365
* This configuration class will activate <em>after</em> the Hibernate auto-configuration.
5466
*
5567
* @author Phillip Webb
5668
* @author Josh Long
5769
* @author Scott Frederick
70+
* @author Stefano Cordio
5871
* @since 1.0.0
5972
* @see EnableJpaRepositories
6073
*/
@@ -64,7 +77,7 @@
6477
@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class, JpaRepositoryConfigExtension.class })
6578
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true",
6679
matchIfMissing = true)
67-
@Import(JpaRepositoriesRegistrar.class)
80+
@Import(JpaRepositoriesImportSelector.class)
6881
@AutoConfigureAfter({ HibernateJpaAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
6982
public class JpaRepositoriesAutoConfiguration {
7083

@@ -106,4 +119,22 @@ static class LazyBootstrapMode {
106119

107120
}
108121

122+
static class JpaRepositoriesImportSelector implements ImportSelector {
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+
}
132+
133+
private String determineImport() {
134+
return ENVERS_AVAILABLE ? EnversRevisionRepositoriesRegistrar.class.getName()
135+
: JpaRepositoriesRegistrar.class.getName();
136+
}
137+
138+
}
139+
109140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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.autoconfigure.data.jpa;
18+
19+
import javax.persistence.EntityManagerFactory;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
25+
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.data.alt.elasticsearch.CityElasticsearchDbRepository;
27+
import org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository;
28+
import org.springframework.boot.autoconfigure.data.alt.mongo.CityMongoDbRepository;
29+
import org.springframework.boot.autoconfigure.data.jpa.city.City;
30+
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
31+
import org.springframework.boot.autoconfigure.data.jpa.country.Country;
32+
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
33+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
34+
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
35+
import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration;
36+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
37+
import org.springframework.context.annotation.Bean;
38+
import org.springframework.context.annotation.ComponentScan.Filter;
39+
import org.springframework.context.annotation.Configuration;
40+
import org.springframework.context.annotation.FilterType;
41+
import org.springframework.context.annotation.Import;
42+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
43+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
44+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
45+
import org.springframework.scheduling.annotation.EnableScheduling;
46+
import org.springframework.transaction.PlatformTransactionManager;
47+
48+
import static org.assertj.core.api.Assertions.assertThat;
49+
50+
/**
51+
* Base class for {@link JpaRepositoriesAutoConfiguration} tests.
52+
*
53+
* @author Dave Syer
54+
* @author Oliver Gierke
55+
* @author Scott Frederick
56+
* @author Stefano Cordio
57+
*/
58+
abstract class AbstractJpaRepositoriesAutoConfigurationTests {
59+
60+
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
61+
.withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class,
62+
JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
63+
.withUserConfiguration(EmbeddedDataSourceConfiguration.class);
64+
65+
@Test
66+
void testDefaultRepositoryConfiguration() {
67+
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
68+
assertThat(context).hasSingleBean(CityRepository.class);
69+
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
70+
assertThat(context).hasSingleBean(EntityManagerFactory.class);
71+
assertThat(context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull();
72+
});
73+
}
74+
75+
@Test
76+
void testOverrideRepositoryConfiguration() {
77+
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
78+
assertThat(context).hasSingleBean(CityJpaRepository.class);
79+
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
80+
assertThat(context).hasSingleBean(EntityManagerFactory.class);
81+
});
82+
}
83+
84+
@Test
85+
void autoConfigurationShouldNotKickInEvenIfManualConfigDidNotCreateAnyRepositories() {
86+
this.contextRunner.withUserConfiguration(SortOfInvalidCustomConfiguration.class)
87+
.run((context) -> assertThat(context).doesNotHaveBean(CityRepository.class));
88+
}
89+
90+
@Test
91+
void whenBootstrapModeIsLazyWithMultipleAsyncExecutorBootstrapExecutorIsConfigured() {
92+
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
93+
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
94+
TaskSchedulingAutoConfiguration.class))
95+
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy")
96+
.run((context) -> assertThat(
97+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor())
98+
.isEqualTo(context.getBean("applicationTaskExecutor")));
99+
}
100+
101+
@Test
102+
void whenBootstrapModeIsLazyWithSingleAsyncExecutorBootstrapExecutorIsConfigured() {
103+
this.contextRunner.withUserConfiguration(SingleAsyncTaskExecutorConfiguration.class)
104+
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=lazy")
105+
.run((context) -> assertThat(
106+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor())
107+
.isEqualTo(context.getBean("testAsyncTaskExecutor")));
108+
}
109+
110+
@Test
111+
void whenBootstrapModeIsDeferredBootstrapExecutorIsConfigured() {
112+
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
113+
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
114+
TaskSchedulingAutoConfiguration.class))
115+
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=deferred")
116+
.run((context) -> assertThat(
117+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor())
118+
.isEqualTo(context.getBean("applicationTaskExecutor")));
119+
}
120+
121+
@Test
122+
void whenBootstrapModeIsDefaultBootstrapExecutorIsNotConfigured() {
123+
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
124+
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
125+
TaskSchedulingAutoConfiguration.class))
126+
.withPropertyValues("spring.data.jpa.repositories.bootstrap-mode=default").run((context) -> assertThat(
127+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull());
128+
}
129+
130+
@Test
131+
void bootstrapModeIsDefaultByDefault() {
132+
this.contextRunner.withUserConfiguration(MultipleAsyncTaskExecutorConfiguration.class)
133+
.withConfiguration(AutoConfigurations.of(TaskExecutionAutoConfiguration.class,
134+
TaskSchedulingAutoConfiguration.class))
135+
.run((context) -> assertThat(
136+
context.getBean(LocalContainerEntityManagerFactoryBean.class).getBootstrapExecutor()).isNull());
137+
}
138+
139+
@Configuration(proxyBeanMethods = false)
140+
@EnableScheduling
141+
@Import(TestConfiguration.class)
142+
static class MultipleAsyncTaskExecutorConfiguration {
143+
144+
}
145+
146+
@Configuration(proxyBeanMethods = false)
147+
@Import(TestConfiguration.class)
148+
static class SingleAsyncTaskExecutorConfiguration {
149+
150+
@Bean
151+
SimpleAsyncTaskExecutor testAsyncTaskExecutor() {
152+
return new SimpleAsyncTaskExecutor();
153+
}
154+
155+
}
156+
157+
@Configuration(proxyBeanMethods = false)
158+
@TestAutoConfigurationPackage(City.class)
159+
static class TestConfiguration {
160+
161+
}
162+
163+
@Configuration(proxyBeanMethods = false)
164+
@EnableJpaRepositories(
165+
basePackageClasses = org.springframework.boot.autoconfigure.data.alt.jpa.CityJpaRepository.class,
166+
excludeFilters = { @Filter(type = FilterType.ASSIGNABLE_TYPE, value = CityMongoDbRepository.class),
167+
@Filter(type = FilterType.ASSIGNABLE_TYPE, value = CityElasticsearchDbRepository.class) })
168+
@TestAutoConfigurationPackage(City.class)
169+
static class CustomConfiguration {
170+
171+
}
172+
173+
@Configuration(proxyBeanMethods = false)
174+
// To not find any repositories
175+
@EnableJpaRepositories("foo.bar")
176+
@TestAutoConfigurationPackage(City.class)
177+
static class SortOfInvalidCustomConfiguration {
178+
179+
}
180+
181+
@Configuration(proxyBeanMethods = false)
182+
@TestAutoConfigurationPackage(Country.class)
183+
static class RevisionRepositoryConfiguration {
184+
185+
}
186+
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.autoconfigure.data.jpa;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.data.jpa.country.CountryRepository;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link JpaRepositoriesAutoConfiguration} with Spring Data Envers on the
27+
* classpath.
28+
*
29+
* @author Stefano Cordio
30+
*/
31+
class EnversRevisionRepositoriesAutoConfigurationTests extends AbstractJpaRepositoriesAutoConfigurationTests {
32+
33+
@Test
34+
void autoConfigurationShouldSucceedWithRevisionRepository() {
35+
this.contextRunner.withUserConfiguration(RevisionRepositoryConfiguration.class)
36+
.run((context) -> assertThat(context).hasSingleBean(CountryRepository.class));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)