Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
IGNITE-13029 Support Spring Data repositories initialization with Spr…
…ing Boot auto-starter - Fixes #41. Signed-off-by: Sergey Chugunov <sergey.chugunov@gmail.com>
- Loading branch information
1 parent
a25117b
commit 7a26b06dbe7db240eb3021e224b94990325bff8a
Showing
18 changed files
with
590 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springframework.boot.autoconfigure; | ||
|
||
import org.apache.ignite.Ignite; | ||
import org.apache.ignite.springdata22.repository.IgniteRepository; | ||
import org.apache.ignite.springdata22.repository.config.EnableIgniteRepositories; | ||
import org.apache.ignite.springdata22.repository.config.IgniteRepositoryConfigurationExtension; | ||
import org.apache.ignite.springdata22.repository.support.IgniteRepositoryFactoryBean; | ||
import org.apache.ignite.springframework.boot.autoconfigure.data.IgniteRepositoriesAutoConfigurationRegistrar; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
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.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
|
||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Ignite Spring Data's Repositories. | ||
* <p> | ||
* Activates when there is a bean of type {@link org.apache.ignite.Ignite} configured in the | ||
* context, the Ignite Spring Data {@link IgniteRepository} type is on the classpath, | ||
* and there is no other, existing {@link IgniteRepository} configured. | ||
* <p> | ||
* Once in effect, the auto-configuration is the equivalent of enabling Ignite repositories | ||
* using the {@link EnableIgniteRepositories @EnableIgniteRepositories} annotation. | ||
* <p> | ||
* This configuration class will activate <em>after</em> the Ignite node auto-configuration. | ||
*/ | ||
@Configuration | ||
@ConditionalOnBean(Ignite.class) | ||
@ConditionalOnClass(IgniteRepository.class) | ||
@ConditionalOnMissingBean({IgniteRepositoryFactoryBean.class, IgniteRepositoryConfigurationExtension.class}) | ||
@Import(IgniteRepositoriesAutoConfigurationRegistrar.class) | ||
@AutoConfigureAfter({IgniteAutoConfiguration.class}) | ||
public class IgniteRepositoryAutoConfiguration { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springframework.boot.autoconfigure.data; | ||
|
||
import java.lang.annotation.Annotation; | ||
import org.apache.ignite.springdata22.repository.config.EnableIgniteRepositories; | ||
import org.apache.ignite.springdata22.repository.config.IgniteRepositoryConfigurationExtension; | ||
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.data.repository.config.RepositoryConfigurationExtension; | ||
|
||
/** | ||
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Ignite Spring Data Repositories. | ||
*/ | ||
public class IgniteRepositoriesAutoConfigurationRegistrar extends AbstractRepositoryConfigurationSourceSupport { | ||
|
||
/** {@inheritDoc} */ | ||
@Override protected Class<? extends Annotation> getAnnotation() { | ||
return EnableIgniteRepositories.class; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override protected Class<?> getConfiguration() { | ||
return EnableIgniteRepositoriesConfiguration.class; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() { | ||
return new IgniteRepositoryConfigurationExtension(); | ||
} | ||
|
||
/** | ||
* The configuration class that will be used by Spring Boot as a template. | ||
*/ | ||
@EnableIgniteRepositories | ||
private static class EnableIgniteRepositoriesConfiguration { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,22 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 description. --> | ||
* Package includes configuration files needed for Ignite Spring Data autoconfiguration. | ||
*/ | ||
package org.apache.ignite.springframework.boot.autoconfigure.data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springframework.boot.autoconfigure; | ||
|
||
import org.apache.ignite.springdata22.repository.config.EnableIgniteRepositories; | ||
import org.apache.ignite.springframework.boot.autoconfigure.misc.DefaultTestConfigutation; | ||
import org.apache.ignite.springframework.boot.autoconfigure.misc.ObjectRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationPackage; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** Tests {@link IgniteRepositoryAutoConfiguration} feature. */ | ||
@ExtendWith(SpringExtension.class) | ||
public class IgniteRepositoriesAutoconfigureTest { | ||
|
||
/** Spring test application context. */ | ||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withUserConfiguration(DefaultTestConfigutation.class) | ||
.withConfiguration(AutoConfigurations.of(IgniteAutoConfiguration.class, IgniteRepositoryAutoConfiguration.class)); | ||
|
||
|
||
/** Test default autoconfiguration */ | ||
@Test | ||
public void testDefaultConfiguration() { | ||
contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> { | ||
assertThat(context).hasSingleBean(ObjectRepository.class); | ||
}); | ||
} | ||
|
||
/** Test configuration with @EnableIgniteRepositories */ | ||
@Test | ||
public void testOverrideConfiguration() { | ||
contextRunner.withUserConfiguration(OverrideConfiguration.class).run((context) -> { | ||
assertThat(context).hasSingleBean(ObjectRepository.class); | ||
}); | ||
} | ||
|
||
/** Test configuration with @EnableIgniteRepositories and invalid base package */ | ||
@Test | ||
public void testInvalidBasePackage() { | ||
contextRunner.withUserConfiguration(InvalidConfiguration.class).run((context) -> { | ||
assertThat(context).doesNotHaveBean(ObjectRepository.class); | ||
}); | ||
} | ||
|
||
|
||
/** */ | ||
@AutoConfigurationPackage | ||
protected static class TestConfiguration { | ||
|
||
} | ||
|
||
/** */ | ||
@EnableIgniteRepositories | ||
protected static class OverrideConfiguration { | ||
|
||
} | ||
|
||
/** */ | ||
@EnableIgniteRepositories(basePackages = "wrong.package") | ||
protected static class InvalidConfiguration { | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springframework.boot.autoconfigure.misc; | ||
|
||
import org.apache.ignite.configuration.CacheConfiguration; | ||
import org.apache.ignite.configuration.IgniteConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Default Ignite configuration for Ignite Spring Data autoconfigure test | ||
*/ | ||
@Configuration | ||
public class DefaultTestConfigutation { | ||
|
||
/** */ | ||
@Bean | ||
public IgniteConfiguration igniteConfiguration() { | ||
return new IgniteConfiguration() | ||
.setCacheConfiguration(new CacheConfiguration<Long, Object>().setName("objectCache")) | ||
.setIgniteInstanceName("test-name") | ||
.setConsistentId("test-id"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.ignite.springframework.boot.autoconfigure.misc; | ||
|
||
import org.apache.ignite.springdata22.repository.IgniteRepository; | ||
import org.apache.ignite.springdata22.repository.config.RepositoryConfig; | ||
|
||
/** | ||
* Test repository. | ||
*/ | ||
@RepositoryConfig(cacheName = "objectCache") | ||
public interface ObjectRepository extends IgniteRepository<Object, Integer> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.