Skip to content

Add auto-configuration support for Spring Data Geode Repositories. #6952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-geode</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2012-2016 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.geode;

import org.apache.geode.cache.Cache;
import org.apache.geode.cache.client.ClientCache;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension;
import org.springframework.data.gemfire.repository.support.GemfireRepositoryFactoryBean;

/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Geode Repositories.
* <p>
* Activates when there is a bean of type {@link Cache} or {@link ClientCache} configured
* in the Spring context, the Spring Data Geode
* {@link org.springframework.data.gemfire.repository.GemfireRepository}
* type is on the classpath, and there is no other, existing
* {@link org.springframework.data.gemfire.repository.GemfireRepository}
* configured.
* <p>
* Once in effect, the auto-configuration is the equivalent of enabling Geode Repositories
* using the
* {@link org.springframework.data.gemfire.repository.config.EnableGemfireRepositories}
* annotation.
*
* @author John Blum
* @since 1.5.0
* @see org.springframework.boot.autoconfigure.data.geode.GeodeRepositoriesAutoConfigureRegistrar
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.data.gemfire.repository.config.EnableGemfireRepositories
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.client.ClientCache
*/
@Configuration
@ConditionalOnBean({ Cache.class, ClientCache.class })
@ConditionalOnClass(GemfireRepository.class)
@ConditionalOnMissingBean({ GemfireRepositoryConfigurationExtension.class, GemfireRepositoryFactoryBean.class })
@ConditionalOnProperty(prefix = "spring.data.geode.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@Import(GeodeRepositoriesAutoConfigureRegistrar.class)
public class GeodeRepositoriesAutoConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2011-2016 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.geode;

import java.lang.annotation.Annotation;

import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
import org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;

/**
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Geode Repositories.
*
* @author John Blum
* @since 1.5.0
* @see org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport
* @see org.springframework.data.gemfire.repository.config.EnableGemfireRepositories
* @see org.springframework.data.gemfire.repository.config.GemfireRepositoryConfigurationExtension
*/
public class GeodeRepositoriesAutoConfigureRegistrar
extends AbstractRepositoryConfigurationSourceSupport {

@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableGemfireRepositories.class;
}

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

@Override
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
return new GemfireRepositoryConfigurationExtension();
}

@EnableGemfireRepositories
private static class EnableGeodeRepositoriesConfiguration {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2011-2016 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.
*/

/**
* Auto-configuration for Spring Data Geode.
*/
package org.springframework.boot.autoconfigure.data.geode;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoC
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.geode.GeodeRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2012-2016 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.alt.geode;

import org.springframework.boot.autoconfigure.data.geode.city.City;
import org.springframework.data.repository.Repository;

public interface CityGeodeRepository extends Repository<City, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright 2011-2016 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.geode;

import org.apache.geode.cache.Cache;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.RegionAttributes;
import org.apache.geode.cache.client.ClientRegionShortcut;

import org.junit.After;
import org.junit.Test;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.data.alt.geode.CityGeodeRepository;
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.autoconfigure.data.geode.city.City;
import org.springframework.boot.autoconfigure.data.geode.city.CityRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.LocalRegionFactoryBean;
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.PeerCacheApplication;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;

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

/**
* Integration test for auto-configuring Spring Data Geode Repository with Spring Boot.
*
* @author John Blum
* @since 1.5.0
*/
public class GeodeRepositoriesAutoConfigurationTests {

private AnnotationConfigApplicationContext applicationContext;

@After
public void tearDown() {
this.applicationContext.close();
}

@Test
public void clientCacheWithDefaultRepositoryConfigurationIsSuccessful() {
prepareApplicationContext(ClientCacheGemFireConfiguration.class);

assertThat(this.applicationContext.getBean(CityRepository.class)).isNotNull();
}

@Test
public void peerCacheWithDefaultRepositoryConfigurationIsSuccessful() {
prepareApplicationContext(PeerCacheGemFireConfiguration.class);

assertThat(this.applicationContext.getBean(CityRepository.class)).isNotNull();
}

@Test(expected = NoSuchBeanDefinitionException.class)
public void noRepositoryConfiguration() {
prepareApplicationContext(EmptyConfiguration.class, PeerCacheGemFireConfiguration.class);

assertThat(this.applicationContext.getBean(Cache.class)).isNotNull();

this.applicationContext.getBean(GemfireRepository.class);
}

@Test(expected = NoSuchBeanDefinitionException.class)
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
prepareApplicationContext(CustomizedConfiguration.class, PeerCacheGemFireConfiguration.class);

assertThat(this.applicationContext.getBean(CityGeodeRepository.class)).isNotNull();

this.applicationContext.getBean(CityRepository.class);
}

protected void prepareApplicationContext(Class<?>... annotatedClasses) {
this.applicationContext = new AnnotationConfigApplicationContext();
this.applicationContext.register(annotatedClasses);
this.applicationContext.register(GeodeRepositoriesAutoConfiguration.class);
this.applicationContext.refresh();
}

@SuppressWarnings("unused")
static abstract class AbstractBaseGemFireConfiguration {

@Bean
@SuppressWarnings("unchecked")
RegionAttributesFactoryBean citiesRegionAttributes() {
RegionAttributesFactoryBean citiesRegionAttributes =
new RegionAttributesFactoryBean();

citiesRegionAttributes.setKeyConstraint(Long.class);
citiesRegionAttributes.setValueConstraint(City.class);

return citiesRegionAttributes;
}
}

@SuppressWarnings("unused")
@TestAutoConfigurationPackage(City.class)
@ClientCacheApplication(name = "GeodeClientCacheApplication", logLevel = "warning")
static class ClientCacheGemFireConfiguration extends AbstractBaseGemFireConfiguration {

@Bean(name = "Cities")
ClientRegionFactoryBean<Long, City> citiesRegion(GemFireCache gemfireCache,
RegionAttributes<Long, City> citiesRegionAttributes) {

ClientRegionFactoryBean<Long, City> citiesRegion =
new ClientRegionFactoryBean<Long, City>();

citiesRegion.setAttributes(citiesRegionAttributes);
citiesRegion.setCache(gemfireCache);
citiesRegion.setClose(false);
citiesRegion.setShortcut(ClientRegionShortcut.LOCAL);

return citiesRegion;
}
}

@SuppressWarnings("unused")
@TestAutoConfigurationPackage(City.class)
@PeerCacheApplication(name = "GeodePeerCacheApplication", logLevel = "warning")
static class PeerCacheGemFireConfiguration extends AbstractBaseGemFireConfiguration {

@Bean(name = "Cities")
LocalRegionFactoryBean<Long, City> citiesRegion(GemFireCache gemfireCache,
RegionAttributes<Long, City> citiesRegionAttributes) {

LocalRegionFactoryBean<Long, City> citiesRegion =
new LocalRegionFactoryBean<Long, City>();

citiesRegion.setAttributes(citiesRegionAttributes);
citiesRegion.setCache(gemfireCache);
citiesRegion.setClose(false);
citiesRegion.setPersistent(false);

return citiesRegion;
}
}

@Configuration
@TestAutoConfigurationPackage(EmptyDataPackage.class)
protected static class EmptyConfiguration {

}

@Configuration
@TestAutoConfigurationPackage(GeodeRepositoriesAutoConfigurationTests.class)
@EnableGemfireRepositories(basePackageClasses = CityGeodeRepository.class)
static class CustomizedConfiguration {

}
}
Loading