Rework properties for enabling Spring Data repositories
Rather than using two properties to enable or disable reactive and imperative repositories for a particular store, this commit introduces a new repository type condition that's backed by a single spring.data.<store>.repositories.type property. The type can be auto (automatically enables whatever's available), imperative (enables imperative repositories), none (enables nothing), or reactive (enables reactive repositories). The default is auto. Repositories do not have a reactive option (such as JPA) continue to have a spring.data.<store>.repositories.enabled property that takes a boolean value. Closes gh-11134pull/11190/merge
parent
8b98db401c
commit
80543250a2
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* {@link Conditional} that only matches when a particular type of Spring Data repository
|
||||
* has been enabled.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnRepositoryTypeCondition.class)
|
||||
public @interface ConditionalOnRepositoryType {
|
||||
|
||||
/**
|
||||
* The name of the store that backs the repositories.
|
||||
* @return the store
|
||||
*/
|
||||
String store();
|
||||
|
||||
/**
|
||||
* The required repository type.
|
||||
* @return the required repository type
|
||||
*/
|
||||
RepositoryType type();
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* {@link SpringBootCondition} for controlling what type of Spring Data repositories are
|
||||
* auto-configured.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class OnRepositoryTypeCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
Map<String, Object> attributes = metadata.getAnnotationAttributes(
|
||||
ConditionalOnRepositoryType.class.getName(), true);
|
||||
RepositoryType configuredType = getTypeProperty(context.getEnvironment(),
|
||||
(String) attributes.get("store"));
|
||||
RepositoryType requiredType = (RepositoryType) attributes.get("type");
|
||||
ConditionMessage.Builder message = ConditionMessage
|
||||
.forCondition(ConditionalOnRepositoryType.class);
|
||||
if (configuredType == requiredType || configuredType == RepositoryType.AUTO) {
|
||||
return ConditionOutcome.match(message.because("configured type of '"
|
||||
+ configuredType.name() + "' matched required type"));
|
||||
}
|
||||
return ConditionOutcome
|
||||
.noMatch(message.because("configured type (" + configuredType.name()
|
||||
+ ") did not match required type (" + requiredType.name() + ")"));
|
||||
}
|
||||
|
||||
private RepositoryType getTypeProperty(Environment environment, String store) {
|
||||
return RepositoryType.valueOf(environment
|
||||
.getProperty(String.format("spring.data.%s.repositories.type", store),
|
||||
"auto")
|
||||
.toUpperCase(Locale.ENGLISH));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
/**
|
||||
* Type of Spring Data repositories to enable.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public enum RepositoryType {
|
||||
|
||||
/**
|
||||
* Enables all repository types automatically based on their availability.
|
||||
*/
|
||||
AUTO,
|
||||
|
||||
/**
|
||||
* Enables imperative repositories.
|
||||
*/
|
||||
IMPERATIVE,
|
||||
|
||||
/**
|
||||
* Enables no repositories.
|
||||
*/
|
||||
NONE,
|
||||
|
||||
/**
|
||||
* Enables reactive repositories.
|
||||
*/
|
||||
REACTIVE;
|
||||
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
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 ConditionalOnRepositoryType}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ConditionalOnRepositoryTypeTests {
|
||||
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void imperativeRepositoryMatchesWithNoConfiguredType() {
|
||||
this.runner.withUserConfiguration(ImperativeRepository.class)
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ImperativeRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactiveRepositoryMatchesWithNoConfiguredType() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class).run(
|
||||
(context) -> assertThat(context).hasSingleBean(ReactiveRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imperativeRepositoryMatchesWithAutoConfiguredType() {
|
||||
this.runner.withUserConfiguration(ImperativeRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:auto")
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ImperativeRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactiveRepositoryMatchesWithAutoConfiguredType() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:auto")
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ReactiveRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imperativeRepositoryMatchesWithImperativeConfiguredType() {
|
||||
this.runner.withUserConfiguration(ImperativeRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:imperative")
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ImperativeRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactiveRepositoryMatchesWithReactiveConfiguredType() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:reactive")
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ReactiveRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imperativeRepositoryDoesNotMatchWithReactiveConfiguredType() {
|
||||
this.runner.withUserConfiguration(ImperativeRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:reactive")
|
||||
.run((context) -> assertThat(context)
|
||||
.doesNotHaveBean(ImperativeRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactiveRepositoryDoesNotMatchWithImperativeConfiguredType() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:imperative")
|
||||
.run((context) -> assertThat(context)
|
||||
.doesNotHaveBean(ReactiveRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imperativeRepositoryDoesNotMatchWithNoneConfiguredType() {
|
||||
this.runner.withUserConfiguration(ImperativeRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:none")
|
||||
.run((context) -> assertThat(context)
|
||||
.doesNotHaveBean(ImperativeRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactiveRepositoryDoesNotMatchWithNoneConfiguredType() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:none")
|
||||
.run((context) -> assertThat(context)
|
||||
.doesNotHaveBean(ReactiveRepository.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failsFastWhenConfiguredTypeIsUnknown() {
|
||||
this.runner.withUserConfiguration(ReactiveRepository.class)
|
||||
.withPropertyValues("spring.data.test.repositories.type:abcde")
|
||||
.run((context) -> assertThat(context).hasFailed());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnRepositoryType(store = "test", type = RepositoryType.IMPERATIVE)
|
||||
protected static class ImperativeRepository {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnRepositoryType(store = "test", type = RepositoryType.REACTIVE)
|
||||
protected static class ReactiveRepository {
|
||||
|
||||
}
|
||||
|
||||
}
|
8
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndBlockingRepositoriesAutoConfigurationTests.java → spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java
8
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndBlockingRepositoriesAutoConfigurationTests.java → spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/couchbase/CouchbaseReactiveAndImperativeRepositoriesAutoConfigurationTests.java
Loading…
Reference in New Issue