Skip to content

Commit b15a6cb

Browse files
committed
Add auto-configuration for Spring Data Envers
1 parent 6efff7b commit b15a6cb

File tree

7 files changed

+216
-3
lines changed

7 files changed

+216
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ dependencies {
112112
optional("org.springframework.batch:spring-batch-core")
113113
optional("org.springframework.data:spring-data-couchbase")
114114
optional("org.springframework.data:spring-data-jpa")
115+
optional("org.springframework.data:spring-data-envers")
115116
optional("org.springframework.data:spring-data-rest-webmvc")
116117
optional("org.springframework.data:spring-data-cassandra")
117118
optional("org.springframework.data:spring-data-elasticsearch") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2020 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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
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;
2930
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3031
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer;
3132
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@@ -35,6 +36,7 @@
3536
import org.springframework.context.annotation.Configuration;
3637
import org.springframework.context.annotation.Import;
3738
import org.springframework.core.task.AsyncTaskExecutor;
39+
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
3840
import org.springframework.data.jpa.repository.JpaRepository;
3941
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4042
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
@@ -52,11 +54,19 @@
5254
* Once in effect, the auto-configuration is the equivalent of enabling JPA repositories
5355
* using the {@link EnableJpaRepositories @EnableJpaRepositories} annotation.
5456
* <p>
57+
* In case
58+
* {@link org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean}
59+
* is on the classpath, it is used instead of
60+
* {@link org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean} to
61+
* support {@link org.springframework.data.repository.history.RevisionRepository} with
62+
* Hibernate Envers.
63+
* <p>
5564
* This configuration class will activate <em>after</em> the Hibernate auto-configuration.
5665
*
5766
* @author Phillip Webb
5867
* @author Josh Long
5968
* @author Scott Frederick
69+
* @author Stefano Cordio
6070
* @since 1.0.0
6171
* @see EnableJpaRepositories
6272
*/
@@ -66,7 +76,6 @@
6676
@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class, JpaRepositoryConfigExtension.class })
6777
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true",
6878
matchIfMissing = true)
69-
@Import(JpaRepositoriesRegistrar.class)
7079
@AutoConfigureAfter({ HibernateJpaAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
7180
public class JpaRepositoriesAutoConfiguration {
7281

@@ -109,4 +118,18 @@ static class LazyBootstrapMode {
109118

110119
}
111120

121+
@Configuration(proxyBeanMethods = false)
122+
@ConditionalOnClass(EnversRevisionRepositoryFactoryBean.class)
123+
@Import(EnversRevisionRepositoriesRegistrar.class)
124+
public static class EnversRevisionRepositoriesRegistrarConfiguration {
125+
126+
}
127+
128+
@Configuration(proxyBeanMethods = false)
129+
@ConditionalOnMissingClass("org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean")
130+
@Import(JpaRepositoriesRegistrar.class)
131+
public static class JpaRepositoriesRegistrarConfiguration {
132+
133+
}
134+
112135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2020 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.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
24+
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.data.jpa.country.Country;
26+
import org.springframework.boot.autoconfigure.data.jpa.country.CountryRepository;
27+
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
28+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
29+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
30+
import org.springframework.context.annotation.Configuration;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests for {@link JpaRepositoriesAutoConfiguration} with Spring Data Envers on the
36+
* classpath.
37+
*
38+
* @author Stefano Cordio
39+
*/
40+
class EnversRevisionRepositoriesAutoConfigurationTests extends JpaRepositoriesAutoConfigurationTests {
41+
42+
@BeforeEach
43+
void setup() {
44+
this.contextRunner = new ApplicationContextRunner()
45+
.withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class,
46+
JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
47+
.withUserConfiguration(EmbeddedDataSourceConfiguration.class);
48+
}
49+
50+
@Test
51+
void autoConfigurationShouldSucceedWithRevisionRepository() {
52+
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
53+
assertThat(context).hasSingleBean(CountryRepository.class);
54+
});
55+
}
56+
57+
@Configuration(proxyBeanMethods = false)
58+
@TestAutoConfigurationPackage(Country.class)
59+
static class TestConfiguration {
60+
61+
}
62+
63+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
3333
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3434
import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration;
35+
import org.springframework.boot.test.context.FilteredClassLoader;
3536
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3637
import org.springframework.context.annotation.Bean;
3738
import org.springframework.context.annotation.ComponentScan.Filter;
3839
import org.springframework.context.annotation.Configuration;
3940
import org.springframework.context.annotation.FilterType;
4041
import org.springframework.context.annotation.Import;
4142
import org.springframework.core.task.SimpleAsyncTaskExecutor;
43+
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
4244
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
4345
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
4446
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -47,15 +49,18 @@
4749
import static org.assertj.core.api.Assertions.assertThat;
4850

4951
/**
50-
* Tests for {@link JpaRepositoriesAutoConfiguration}.
52+
* Tests for {@link JpaRepositoriesAutoConfiguration} without Spring Data Envers on the
53+
* classpath.
5154
*
5255
* @author Dave Syer
5356
* @author Oliver Gierke
5457
* @author Scott Frederick
58+
* @author Stefano Cordio
5559
*/
5660
class JpaRepositoriesAutoConfigurationTests {
5761

58-
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
62+
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
63+
.withClassLoader(new FilteredClassLoader(EnversRevisionRepositoryFactoryBean.class))
5964
.withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class,
6065
JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
6166
.withUserConfiguration(EmbeddedDataSourceConfiguration.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2019 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.country;
18+
19+
import org.hibernate.envers.Audited;
20+
21+
import javax.persistence.Column;
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
import java.io.Serializable;
26+
27+
@Entity
28+
public class Country implements Serializable {
29+
30+
private static final long serialVersionUID = 1L;
31+
32+
@Id
33+
@GeneratedValue
34+
private Long id;
35+
36+
@Audited
37+
@Column
38+
private String name;
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
public void setId(Long id) {
45+
this.id = id;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public void setName(String name) {
53+
this.name = name;
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2012-2019 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.country;
18+
19+
import org.springframework.data.jpa.repository.JpaRepository;
20+
import org.springframework.data.repository.history.RevisionRepository;
21+
22+
public interface CountryRepository extends JpaRepository<Country, Long>, RevisionRepository<Country, Long, Integer> {
23+
24+
}

0 commit comments

Comments
 (0)