Skip to content
Permalink
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
dorozhKing authored and sergey-chugunov-1985 committed Apr 19, 2021
1 parent a25117b commit 7a26b06dbe7db240eb3021e224b94990325bff8a
Showing 18 changed files with 590 additions and 4 deletions.
@@ -56,6 +56,20 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data_2.2-ext</artifactId>
<version>${ignite.springdata.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring.data-2.2.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</artifactId>
@@ -95,7 +95,7 @@ public IgniteConfiguration igniteConfiguration(IgniteConfigurer configurer) {
*/
@ConditionalOnBean(IgniteConfiguration.class)
@Bean
public Ignite ignite(IgniteConfiguration cfg) {
public Ignite igniteInstance(IgniteConfiguration cfg) {
return Ignition.start(cfg);
}
}
@@ -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 {

}
@@ -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 {

}
}
@@ -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;
@@ -14,4 +14,5 @@
# limitations under the License.

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.apache.ignite.springframework.boot.autoconfigure.IgniteAutoConfiguration
org.apache.ignite.springframework.boot.autoconfigure.IgniteAutoConfiguration,\
org.apache.ignite.springframework.boot.autoconfigure.IgniteRepositoryAutoConfiguration
@@ -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 {

}


}
@@ -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");
}
}
@@ -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> {

}
@@ -57,6 +57,20 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-spring-data_2.2-ext</artifactId>
<version>${ignite.springdata.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring.data-2.2.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-log4j</artifactId>
@@ -78,7 +78,7 @@ public IgniteClientConfigurer clientConfigurer() {
@ConditionalOnMissingBean
@Bean
@ConfigurationProperties(prefix = IGNITE_CLIENT_PROPS_PREFIX)
public ClientConfiguration clientConfiguration(IgniteClientConfigurer configurer) {
public ClientConfiguration igniteCfg(IgniteClientConfigurer configurer) {
ClientConfiguration cfg = new ClientConfiguration();

configurer.accept(cfg);

0 comments on commit 7a26b06

Please sign in to comment.