Skip to content

Commit d580ad4

Browse files
mp911deschauder
authored andcommitted
Introduce EnableEnversRepositories meta-annotation.
We now provide a composed annotation to enable Envers repositories without the need to specify a custom repository base class. Closes #289 Original pull request #290
1 parent bcfc55c commit d580ad4

File tree

6 files changed

+212
-9
lines changed

6 files changed

+212
-9
lines changed

README.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ image:https://spring.io/badges/spring-data-envers/snapshot.svg[title=Spring Data
33

44
= Spring Data Envers image:https://jenkins.spring.io/buildStatus/icon?job=spring-data-envers%2Fmaster&subject=Build[link=https://jenkins.spring.io/view/SpringData/job/spring-data-envers/] https://gitter.im/spring-projects/spring-data[image:https://badges.gitter.im/spring-projects/spring-data.svg[Gitter]]
55

6-
This project is an extension of the https://github.com/SpringSource/spring-data-jpa[Spring Data JPA] project to allow access to entity revisions managed by Hibernate Envers. The sources mostly originate from a contribution of Philipp Hügelmeyer https://github.com/hygl[@hygl].
6+
This project is an extension of the https://github.com/SpringSource/spring-data-jpa[Spring Data JPA] project to allow access to entity revisions managed by Hibernate Envers.The sources mostly originate from a contribution of Philipp Hügelmeyer https://github.com/hygl[@hygl].
77

88
The core feature of the module consists of an implementation of the `RevisionRepository` of Spring Data Commons.
99

@@ -35,12 +35,12 @@ interface PersonRepository extends RevisionRepository<Person, Long, Integer>, Cr
3535
}
3636
----
3737

38-
To successfully activate the Spring Data Envers repository factory, use the Spring Data JPA's `@EnableJpaRepositories` annotation, specifying the `repositoryFactoryBeanClass` attribute:
38+
To activate the Spring Data Envers repositories, use the Spring Data Envers' `EnableEnversRepositories` annotation:
3939

4040
[source,java]
4141
----
4242
@Configuration
43-
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
43+
@EnableEnversRepositories
4444
public class ApplicationConfiguration {
4545
4646
// Your configuration beans

src/main/asciidoc/envers.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ To enable Spring Data Envers and Spring Data JPA, we need to configure two beans
4040
[source,java]
4141
----
4242
@Configuration
43-
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class) // <1>
43+
@EnableEnversRepositories
4444
@EnableTransactionManagement
4545
public class EnversDemoConfiguration {
4646
@@ -73,7 +73,6 @@ public class EnversDemoConfiguration {
7373
}
7474
}
7575
----
76-
<1> This is the only difference from a normal Spring Data JPA configuration. `EnversRevisionRepositoryFactoryBean` ensures implementations of the methods in `RevisionRepository` are available.
7776
====
7877

7978
To actually use Spring Data Envers, make one or more repositories into a {spring-data-commons-javadoc-base}/org/springframework/data/repository/history/RevisionRepository.html[`RevisionRepository`] by adding it as an extended interface:

src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= Spring Data Envers - Reference Documentation
2-
Oliver Gierke; Jens Schauder
2+
Oliver Gierke; Jens Schauder; Mark Paluch
33
:revnumber: {version}
44
:revdate: {localdate}
55
:javadoc-base: https://docs.spring.io/spring-data/envers/docs/{revnumber}/api/
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.envers.repository.config;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import javax.persistence.EntityManagerFactory;
26+
27+
import org.springframework.beans.factory.FactoryBean;
28+
import org.springframework.context.annotation.ComponentScan.Filter;
29+
import org.springframework.context.annotation.Lazy;
30+
import org.springframework.core.annotation.AliasFor;
31+
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
32+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
33+
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
34+
import org.springframework.data.repository.config.BootstrapMode;
35+
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
36+
import org.springframework.data.repository.query.QueryLookupStrategy;
37+
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
38+
import org.springframework.transaction.PlatformTransactionManager;
39+
40+
/**
41+
* Annotation to enable Envers repositories. Will scan the package of the annotated configuration class for Spring Data
42+
* repositories by default.
43+
* <p>
44+
* This annotation is a meta-annotation for {@link EnableJpaRepositories @EnableJpaRepositories} overriding the default
45+
* {@link #repositoryFactoryBeanClass} to {@link EnversRevisionRepositoryFactoryBean}.
46+
*
47+
* @author Mark Paluch
48+
* @since 2.5
49+
* @see EnableJpaRepositories
50+
* @see AliasFor
51+
*/
52+
@Target(ElementType.TYPE)
53+
@Retention(RetentionPolicy.RUNTIME)
54+
@Documented
55+
@Inherited
56+
@EnableJpaRepositories
57+
public @interface EnableEnversRepositories {
58+
59+
/**
60+
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
61+
* {@code @EnableJpaRepositories("org.my.pkg")} instead of
62+
* {@code @EnableEnversRepositories(basePackages="org.my.pkg")}.
63+
*/
64+
@AliasFor(annotation = EnableJpaRepositories.class)
65+
String[] value() default {};
66+
67+
/**
68+
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
69+
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
70+
*/
71+
@AliasFor(annotation = EnableJpaRepositories.class)
72+
String[] basePackages() default {};
73+
74+
/**
75+
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
76+
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
77+
* each package that serves no purpose other than being referenced by this attribute.
78+
*/
79+
@AliasFor(annotation = EnableJpaRepositories.class)
80+
Class<?>[] basePackageClasses() default {};
81+
82+
/**
83+
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
84+
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
85+
*/
86+
@AliasFor(annotation = EnableJpaRepositories.class)
87+
Filter[] includeFilters() default {};
88+
89+
/**
90+
* Specifies which types are not eligible for component scanning.
91+
*/
92+
@AliasFor(annotation = EnableJpaRepositories.class)
93+
Filter[] excludeFilters() default {};
94+
95+
/**
96+
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
97+
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
98+
* for {@code PersonRepositoryImpl}.
99+
*
100+
* @return
101+
*/
102+
@AliasFor(annotation = EnableJpaRepositories.class)
103+
String repositoryImplementationPostfix() default "Impl";
104+
105+
/**
106+
* Configures the location of where to find the Spring Data named queries properties file. Will default to
107+
* {@code META-INF/jpa-named-queries.properties}.
108+
*
109+
* @return
110+
*/
111+
@AliasFor(annotation = EnableJpaRepositories.class)
112+
String namedQueriesLocation() default "";
113+
114+
/**
115+
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
116+
* {@link Key#CREATE_IF_NOT_FOUND}.
117+
*
118+
* @return
119+
*/
120+
@AliasFor(annotation = EnableJpaRepositories.class)
121+
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
122+
123+
/**
124+
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
125+
* {@link JpaRepositoryFactoryBean}.
126+
*
127+
* @return
128+
*/
129+
@AliasFor(annotation = EnableJpaRepositories.class)
130+
Class<?> repositoryFactoryBeanClass() default EnversRevisionRepositoryFactoryBean.class;
131+
132+
/**
133+
* Configure the repository base class to be used to create repository proxies for this particular configuration.
134+
*
135+
* @return
136+
*/
137+
@AliasFor(annotation = EnableJpaRepositories.class)
138+
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
139+
140+
// JPA specific configuration
141+
142+
/**
143+
* Configures the name of the {@link EntityManagerFactory} bean definition to be used to create repositories
144+
* discovered through this annotation. Defaults to {@code entityManagerFactory}.
145+
*
146+
* @return
147+
*/
148+
@AliasFor(annotation = EnableJpaRepositories.class)
149+
String entityManagerFactoryRef() default "entityManagerFactory";
150+
151+
/**
152+
* Configures the name of the {@link PlatformTransactionManager} bean definition to be used to create repositories
153+
* discovered through this annotation. Defaults to {@code transactionManager}.
154+
*
155+
* @return
156+
*/
157+
@AliasFor(annotation = EnableJpaRepositories.class)
158+
String transactionManagerRef() default "transactionManager";
159+
160+
/**
161+
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
162+
* repositories infrastructure.
163+
*/
164+
@AliasFor(annotation = EnableJpaRepositories.class)
165+
boolean considerNestedRepositories() default false;
166+
167+
/**
168+
* Configures whether to enable default transactions for Spring Data JPA repositories. Defaults to {@literal true}. If
169+
* disabled, repositories must be used behind a facade that's configuring transactions (e.g. using Spring's annotation
170+
* driven transaction facilities) or repository methods have to be used to demarcate transactions.
171+
*
172+
* @return whether to enable default transactions, defaults to {@literal true}.
173+
*/
174+
@AliasFor(annotation = EnableJpaRepositories.class)
175+
boolean enableDefaultTransactions() default true;
176+
177+
/**
178+
* Configures when the repositories are initialized in the bootstrap lifecycle. {@link BootstrapMode#DEFAULT}
179+
* (default) means eager initialization except all repository interfaces annotated with {@link Lazy},
180+
* {@link BootstrapMode#LAZY} means lazy by default including injection of lazy-initialization proxies into client
181+
* beans so that those can be instantiated but will only trigger the initialization upon first repository usage (i.e a
182+
* method invocation on it). This means repositories can still be uninitialized when the application context has
183+
* completed its bootstrap. {@link BootstrapMode#DEFERRED} is fundamentally the same as {@link BootstrapMode#LAZY},
184+
* but triggers repository initialization when the application context finishes its bootstrap.
185+
*
186+
* @return
187+
*/
188+
@AliasFor(annotation = EnableJpaRepositories.class)
189+
BootstrapMode bootstrapMode() default BootstrapMode.DEFAULT;
190+
191+
/**
192+
* Configures what character is used to escape the wildcards {@literal _} and {@literal %} in derived queries with
193+
* {@literal contains}, {@literal startsWith} or {@literal endsWith} clauses.
194+
*
195+
* @return a single character used for escaping.
196+
*/
197+
@AliasFor(annotation = EnableJpaRepositories.class)
198+
char escapeCharacter() default '\\';
199+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Classes for Envers Repositories configuration support.
3+
*/
4+
@org.springframework.lang.NonNullApi
5+
package org.springframework.data.envers.repository.config;

src/test/java/org/springframework/data/envers/Config.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import javax.sql.DataSource;
2323

2424
import org.hibernate.envers.strategy.ValidityAuditStrategy;
25+
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
27-
import org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean;
28-
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
28+
import org.springframework.data.envers.repository.config.EnableEnversRepositories;
2929
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
3030
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
3131
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
@@ -41,7 +41,7 @@
4141
* @author Oliver Gierke
4242
*/
4343
@Configuration
44-
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
44+
@EnableEnversRepositories
4545
public class Config {
4646

4747
@Bean

0 commit comments

Comments
 (0)