Skip to content

Commit

Permalink
Add auto-configuration for Spring Data Envers
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Aug 4, 2020
1 parent 6efff7b commit b15a6cb
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 3 deletions.
1 change: 1 addition & 0 deletions spring-boot-project/spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ dependencies {
optional("org.springframework.batch:spring-batch-core")
optional("org.springframework.data:spring-data-couchbase")
optional("org.springframework.data:spring-data-jpa")
optional("org.springframework.data:spring-data-envers")
optional("org.springframework.data:spring-data-rest-webmvc")
optional("org.springframework.data:spring-data-cassandra")
optional("org.springframework.data:spring-data-elasticsearch") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.data.jpa;

import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Envers
* Repositories.
*
* @author Stefano Cordio
*/
class EnversRevisionRepositoriesRegistrar extends JpaRepositoriesRegistrar {

@Override
protected Class<?> getConfiguration() {
return EnableJpaRepositoriesConfiguration.class;
}

@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
private static class EnableJpaRepositoriesConfiguration {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
Expand All @@ -35,6 +36,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension;
Expand All @@ -52,11 +54,19 @@
* Once in effect, the auto-configuration is the equivalent of enabling JPA repositories
* using the {@link EnableJpaRepositories @EnableJpaRepositories} annotation.
* <p>
* In case
* {@link org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean}
* is on the classpath, it is used instead of
* {@link org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean} to
* support {@link org.springframework.data.repository.history.RevisionRepository} with
* Hibernate Envers.
* <p>
* This configuration class will activate <em>after</em> the Hibernate auto-configuration.
*
* @author Phillip Webb
* @author Josh Long
* @author Scott Frederick
* @author Stefano Cordio
* @since 1.0.0
* @see EnableJpaRepositories
*/
Expand All @@ -66,7 +76,6 @@
@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class, JpaRepositoryConfigExtension.class })
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true",
matchIfMissing = true)
@Import(JpaRepositoriesRegistrar.class)
@AutoConfigureAfter({ HibernateJpaAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
public class JpaRepositoriesAutoConfiguration {

Expand Down Expand Up @@ -109,4 +118,18 @@ static class LazyBootstrapMode {

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(EnversRevisionRepositoryFactoryBean.class)
@Import(EnversRevisionRepositoriesRegistrar.class)
public static class EnversRevisionRepositoriesRegistrarConfiguration {

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean")
@Import(JpaRepositoriesRegistrar.class)
public static class JpaRepositoriesRegistrarConfiguration {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.data.jpa;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.country.Country;
import org.springframework.boot.autoconfigure.data.jpa.country.CountryRepository;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link JpaRepositoriesAutoConfiguration} with Spring Data Envers on the
* classpath.
*
* @author Stefano Cordio
*/
class EnversRevisionRepositoriesAutoConfigurationTests extends JpaRepositoriesAutoConfigurationTests {

@BeforeEach
void setup() {
this.contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
.withUserConfiguration(EmbeddedDataSourceConfiguration.class);
}

@Test
void autoConfigurationShouldSucceedWithRevisionRepository() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(CountryRepository.class);
});
}

@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(Country.class)
static class TestConfiguration {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand All @@ -47,15 +49,18 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link JpaRepositoriesAutoConfiguration}.
* Tests for {@link JpaRepositoriesAutoConfiguration} without Spring Data Envers on the
* classpath.
*
* @author Dave Syer
* @author Oliver Gierke
* @author Scott Frederick
* @author Stefano Cordio
*/
class JpaRepositoriesAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withClassLoader(new FilteredClassLoader(EnversRevisionRepositoryFactoryBean.class))
.withConfiguration(AutoConfigurations.of(HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class))
.withUserConfiguration(EmbeddedDataSourceConfiguration.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.data.jpa.country;

import org.hibernate.envers.Audited;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;

@Entity
public class Country implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;

@Audited
@Column
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.data.jpa.country;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.history.RevisionRepository;

public interface CountryRepository extends JpaRepository<Country, Long>, RevisionRepository<Country, Long, Integer> {

}

0 comments on commit b15a6cb

Please sign in to comment.