Repository: ignite Updated Branches: refs/heads/ignite-3547 220d68b37 -> 3691079d2
IGNITE-3476 Node started within SpringCacheManager does not inject Spring resources Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/a39770aa Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/a39770aa Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/a39770aa Branch: refs/heads/ignite-3547 Commit: a39770aa48412fad57712a4cb580b028cb111eb9 Parents: 71ef652 Author: EdShangGG <eshangar...@gridgain.com> Authored: Tue Jul 26 12:46:05 2016 +0300 Committer: EdShangGG <eshangar...@gridgain.com> Committed: Tue Jul 26 12:46:05 2016 +0300 ---------------------------------------------------------------------- .../ignite/cache/spring/SpringCacheManager.java | 17 ++- .../spring/SpringTransactionManager.java | 59 +++++---- .../test/java/config/spring-transactions.xml | 4 +- .../ignite/TestInjectionLifecycleBean.java | 42 +++++++ .../org/apache/ignite/spring-injection-test.xml | 43 +++++++ .../SpringCacheManagerContextInjectionTest.java | 126 +++++++++++++++++++ .../testsuites/IgniteSpringTestSuite.java | 5 + ...gTransactionManagerContextInjectionTest.java | 125 ++++++++++++++++++ 8 files changed, 393 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java index 71b723f..6d1a9b6 100644 --- a/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java +++ b/modules/spring/src/main/java/org/apache/ignite/cache/spring/SpringCacheManager.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.concurrent.ConcurrentMap; import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteSpring; import org.apache.ignite.Ignition; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; @@ -28,6 +29,8 @@ import org.apache.ignite.configuration.NearCacheConfiguration; import org.jsr166.ConcurrentHashMap8; import org.springframework.beans.factory.InitializingBean; import org.springframework.cache.CacheManager; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; /** * Implementation of Spring cache abstraction based on Ignite cache. @@ -133,7 +136,7 @@ import org.springframework.cache.CacheManager; * Ignite distribution, and all these nodes will participate * in caching the data. */ -public class SpringCacheManager implements CacheManager, InitializingBean { +public class SpringCacheManager implements CacheManager, InitializingBean, ApplicationContextAware { /** Caches map. */ private final ConcurrentMap<String, SpringCache> caches = new ConcurrentHashMap8<>(); @@ -155,6 +158,14 @@ public class SpringCacheManager implements CacheManager, InitializingBean { /** Ignite instance. */ private Ignite ignite; + /** Spring context */ + private ApplicationContext springCtx; + + /** {@inheritDoc} */ + @Override public void setApplicationContext(ApplicationContext ctx) { + this.springCtx = ctx; + } + /** * Gets configuration file path. * @@ -257,9 +268,9 @@ public class SpringCacheManager implements CacheManager, InitializingBean { } if (cfgPath != null) - ignite = Ignition.start(cfgPath); + ignite = IgniteSpring.start(cfgPath, springCtx); else if (cfg != null) - ignite = Ignition.start(cfg); + ignite = IgniteSpring.start(cfg, springCtx); else ignite = Ignition.ignite(gridName); } http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/main/java/org/apache/ignite/transactions/spring/SpringTransactionManager.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/main/java/org/apache/ignite/transactions/spring/SpringTransactionManager.java b/modules/spring/src/main/java/org/apache/ignite/transactions/spring/SpringTransactionManager.java index d8bbbbd..32a7b43 100644 --- a/modules/spring/src/main/java/org/apache/ignite/transactions/spring/SpringTransactionManager.java +++ b/modules/spring/src/main/java/org/apache/ignite/transactions/spring/SpringTransactionManager.java @@ -21,12 +21,15 @@ import java.util.concurrent.TimeUnit; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; +import org.apache.ignite.IgniteSpring; import org.apache.ignite.Ignition; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.transactions.Transaction; import org.apache.ignite.transactions.TransactionConcurrency; import org.apache.ignite.transactions.TransactionIsolation; import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.transaction.CannotCreateTransactionException; import org.springframework.transaction.InvalidIsolationLevelException; import org.springframework.transaction.PlatformTransactionManager; @@ -194,7 +197,7 @@ import org.springframework.transaction.support.TransactionSynchronizationManager * </pre> */ public class SpringTransactionManager extends AbstractPlatformTransactionManager - implements ResourceTransactionManager, PlatformTransactionManager, InitializingBean { + implements ResourceTransactionManager, PlatformTransactionManager, InitializingBean, ApplicationContextAware { /** * Logger. */ @@ -225,6 +228,14 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager */ private Ignite ignite; + /** Spring context */ + private ApplicationContext springCtx; + + /** {@inheritDoc} */ + @Override public void setApplicationContext(ApplicationContext ctx) { + this.springCtx = ctx; + } + /** * Constructs the transaction manager with no target Ignite instance. An * instance must be set before use. @@ -319,9 +330,9 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager } if (cfgPath != null) - ignite = Ignition.start(cfgPath); + ignite = IgniteSpring.start(cfgPath, springCtx); else if (cfg != null) - ignite = Ignition.start(cfg); + ignite = IgniteSpring.start(cfg, springCtx); else ignite = Ignition.ignite(gridName); @@ -335,12 +346,12 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager * {@inheritDoc} */ @Override protected Object doGetTransaction() throws TransactionException { - IgniteTransactionObject txObject = new IgniteTransactionObject(); + IgniteTransactionObject txObj = new IgniteTransactionObject(); - txObject.setTransactionHolder( + txObj.setTransactionHolder( (IgniteTransactionHolder)TransactionSynchronizationManager.getResource(this.ignite), false); - return txObject; + return txObj; } /** @@ -350,11 +361,11 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager if (definition.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED) throw new InvalidIsolationLevelException("Ignite does not support READ_UNCOMMITTED isolation level."); - IgniteTransactionObject txObject = (IgniteTransactionObject)transaction; + IgniteTransactionObject txObj = (IgniteTransactionObject)transaction; Transaction tx = null; try { - if (txObject.getTransactionHolder() == null || txObject.getTransactionHolder().isSynchronizedWithTransaction()) { + if (txObj.getTransactionHolder() == null || txObj.getTransactionHolder().isSynchronizedWithTransaction()) { long timeout = ignite.configuration().getTransactionConfiguration().getDefaultTxTimeout(); if (definition.getTimeout() > 0) @@ -366,17 +377,17 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager if (log.isDebugEnabled()) log.debug("Started Ignite transaction: " + newTx); - txObject.setTransactionHolder(new IgniteTransactionHolder(newTx), true); + txObj.setTransactionHolder(new IgniteTransactionHolder(newTx), true); } - txObject.getTransactionHolder().setSynchronizedWithTransaction(true); - txObject.getTransactionHolder().setTransactionActive(true); + txObj.getTransactionHolder().setSynchronizedWithTransaction(true); + txObj.getTransactionHolder().setTransactionActive(true); - tx = txObject.getTransactionHolder().getTransaction(); + tx = txObj.getTransactionHolder().getTransaction(); // Bind the session holder to the thread. - if (txObject.isNewTransactionHolder()) - TransactionSynchronizationManager.bindResource(this.ignite, txObject.getTransactionHolder()); + if (txObj.isNewTransactionHolder()) + TransactionSynchronizationManager.bindResource(this.ignite, txObj.getTransactionHolder()); } catch (Exception ex) { if (tx != null) @@ -390,8 +401,8 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager * {@inheritDoc} */ @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException { - IgniteTransactionObject txObject = (IgniteTransactionObject)status.getTransaction(); - Transaction tx = txObject.getTransactionHolder().getTransaction(); + IgniteTransactionObject txObj = (IgniteTransactionObject)status.getTransaction(); + Transaction tx = txObj.getTransactionHolder().getTransaction(); if (status.isDebug() && log.isDebugEnabled()) log.debug("Committing Ignite transaction: " + tx); @@ -408,8 +419,8 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager * {@inheritDoc} */ @Override protected void doRollback(DefaultTransactionStatus status) throws TransactionException { - IgniteTransactionObject txObject = (IgniteTransactionObject)status.getTransaction(); - Transaction tx = txObject.getTransactionHolder().getTransaction(); + IgniteTransactionObject txObj = (IgniteTransactionObject)status.getTransaction(); + Transaction tx = txObj.getTransactionHolder().getTransaction(); if (status.isDebug() && log.isDebugEnabled()) log.debug("Rolling back Ignite transaction: " + tx); @@ -426,27 +437,27 @@ public class SpringTransactionManager extends AbstractPlatformTransactionManager * {@inheritDoc} */ @Override protected void doCleanupAfterCompletion(Object transaction) { - IgniteTransactionObject txObject = (IgniteTransactionObject)transaction; + IgniteTransactionObject txObj = (IgniteTransactionObject)transaction; // Remove the transaction holder from the thread, if exposed. - if (txObject.isNewTransactionHolder()) { - Transaction tx = txObject.getTransactionHolder().getTransaction(); + if (txObj.isNewTransactionHolder()) { + Transaction tx = txObj.getTransactionHolder().getTransaction(); TransactionSynchronizationManager.unbindResource(this.ignite); if (log.isDebugEnabled()) log.debug("Releasing Ignite transaction: " + tx); } - txObject.getTransactionHolder().clear(); + txObj.getTransactionHolder().clear(); } /** * {@inheritDoc} */ @Override protected boolean isExistingTransaction(Object transaction) throws TransactionException { - IgniteTransactionObject txObject = (IgniteTransactionObject)transaction; + IgniteTransactionObject txObj = (IgniteTransactionObject)transaction; - return (txObject.getTransactionHolder() != null && txObject.getTransactionHolder().isTransactionActive()); + return (txObj.getTransactionHolder() != null && txObj.getTransactionHolder().isTransactionActive()); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/config/spring-transactions.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/config/spring-transactions.xml b/modules/spring/src/test/java/config/spring-transactions.xml index ba90cb0..392bd84 100644 --- a/modules/spring/src/test/java/config/spring-transactions.xml +++ b/modules/spring/src/test/java/config/spring-transactions.xml @@ -27,7 +27,9 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <tx:annotation-driven/> - <context:component-scan base-package="org.apache.ignite.transactions.spring"/> + <context:component-scan base-package="org.apache.ignite.transactions.spring"> + <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/> + </context:component-scan> <bean id="transactionManager" class="org.apache.ignite.transactions.spring.SpringTransactionManager"> <property name="transactionConcurrency" value="OPTIMISTIC"/> http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/org/apache/ignite/TestInjectionLifecycleBean.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/TestInjectionLifecycleBean.java b/modules/spring/src/test/java/org/apache/ignite/TestInjectionLifecycleBean.java new file mode 100644 index 0000000..2b8c932 --- /dev/null +++ b/modules/spring/src/test/java/org/apache/ignite/TestInjectionLifecycleBean.java @@ -0,0 +1,42 @@ +/* + * 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; + +import org.apache.ignite.lifecycle.LifecycleBean; +import org.apache.ignite.lifecycle.LifecycleEventType; +import org.apache.ignite.resources.SpringApplicationContextResource; +import org.springframework.context.ApplicationContext; + +import static org.junit.Assert.assertNotNull; + +/** Lifecycle bean for testing. */ +public class TestInjectionLifecycleBean implements LifecycleBean { + /** */ + @SpringApplicationContextResource + private ApplicationContext appCtx; + + /** Checks that context was injected. */ + public void checkState() { + assertNotNull(appCtx); + } + + /** {@inheritDoc} */ + @Override public void onLifecycleEvent(LifecycleEventType evt) { + checkState(); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/org/apache/ignite/spring-injection-test.xml ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring-injection-test.xml b/modules/spring/src/test/java/org/apache/ignite/spring-injection-test.xml new file mode 100644 index 0000000..b8368a3 --- /dev/null +++ b/modules/spring/src/test/java/org/apache/ignite/spring-injection-test.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + 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. +--> + +<!-- + GridGain Spring configuration file to startup grid cache. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:util="http://www.springframework.org/schema/util" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util + http://www.springframework.org/schema/util/spring-util.xsd"> + <bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> + <property name="lifecycleBeans"> + <array> + <bean id="bean1" class="org.apache.ignite.TestInjectionLifecycleBean"/> + <bean id="bean2" class="org.apache.ignite.TestInjectionLifecycleBean"/> + </array> + </property> + + <property name="localHost" value="127.0.0.1"/> + + <property name="gridName" value="springInjectionTest"/> + </bean> +</beans> http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/org/apache/ignite/spring/SpringCacheManagerContextInjectionTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/SpringCacheManagerContextInjectionTest.java b/modules/spring/src/test/java/org/apache/ignite/spring/SpringCacheManagerContextInjectionTest.java new file mode 100644 index 0000000..1554198 --- /dev/null +++ b/modules/spring/src/test/java/org/apache/ignite/spring/SpringCacheManagerContextInjectionTest.java @@ -0,0 +1,126 @@ +/* + * 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.spring; + +import org.apache.ignite.Ignite; +import org.apache.ignite.TestInjectionLifecycleBean; +import org.apache.ignite.cache.spring.SpringCacheManager; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.lifecycle.LifecycleBean; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * + */ +public class SpringCacheManagerContextInjectionTest extends GridCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testBeanInjectionUsingConfigPath() throws Exception { + new AnnotationConfigApplicationContext(TestPathConfiguration.class); + + Ignite grid = IgnitionEx.grid("springInjectionTest"); + + IgniteConfiguration cfg = grid.configuration(); + + LifecycleBean[] beans = cfg.getLifecycleBeans(); + + assertEquals(2, beans.length); + + TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean)beans[0]; + TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean)beans[1]; + + bean1.checkState(); + bean2.checkState(); + } + + /** + * @throws Exception If failed. + */ + public void testBeanInjectionUsingConfiguration() throws Exception { + BeanFactory factory = new AnnotationConfigApplicationContext(TestCfgConfiguration.class); + + TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean)factory.getBean("bean1"); + TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean)factory.getBean("bean2"); + + bean1.checkState(); + bean2.checkState(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** */ + @SuppressWarnings("WeakerAccess") + @Configuration + static class TestPathConfiguration { + /** */ + @Bean(name = "mgr") + public SpringCacheManager springCacheManager() { + SpringCacheManager mgr = new SpringCacheManager(); + + mgr.setConfigurationPath("org/apache/ignite/spring-injection-test.xml"); + + return mgr; + } + } + + /** */ + @SuppressWarnings("WeakerAccess") + @Configuration + static class TestCfgConfiguration { + /** */ + @Bean(name = "mgr") + public SpringCacheManager springCacheManager() { + IgniteConfiguration cfg = new IgniteConfiguration(); + + cfg.setLocalHost("127.0.0.1"); + + cfg.setGridName("scmt"); + + cfg.setLifecycleBeans(bean1(), bean2()); + + SpringCacheManager mgr = new SpringCacheManager(); + + mgr.setConfiguration(cfg); + + return mgr; + } + + /** */ + @Bean(name = "bean1") + LifecycleBean bean1() { + return new TestInjectionLifecycleBean(); + } + + /** */ + @Bean(name = "bean2") + LifecycleBean bean2() { + return new TestInjectionLifecycleBean(); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java index 4fc159e..9ae3423 100644 --- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java +++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java @@ -28,8 +28,10 @@ import org.apache.ignite.p2p.GridP2PUserVersionChangeSelfTest; import org.apache.ignite.spring.GridSpringCacheManagerSelfTest; import org.apache.ignite.spring.IgniteExcludeInConfigurationTest; import org.apache.ignite.spring.IgniteStartFromStreamConfigurationTest; +import org.apache.ignite.spring.SpringCacheManagerContextInjectionTest; import org.apache.ignite.spring.injection.GridServiceInjectionSpringResourceTest; import org.apache.ignite.transactions.spring.GridSpringTransactionManagerSelfTest; +import org.apache.ignite.transactions.spring.SpringTransactionManagerContextInjectionTest; /** * Spring tests. @@ -68,6 +70,9 @@ public class IgniteSpringTestSuite extends TestSuite { suite.addTestSuite(GridServiceInjectionSpringResourceTest.class); + suite.addTestSuite(SpringCacheManagerContextInjectionTest.class); + suite.addTestSuite(SpringTransactionManagerContextInjectionTest.class); + return suite; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/a39770aa/modules/spring/src/test/java/org/apache/ignite/transactions/spring/SpringTransactionManagerContextInjectionTest.java ---------------------------------------------------------------------- diff --git a/modules/spring/src/test/java/org/apache/ignite/transactions/spring/SpringTransactionManagerContextInjectionTest.java b/modules/spring/src/test/java/org/apache/ignite/transactions/spring/SpringTransactionManagerContextInjectionTest.java new file mode 100644 index 0000000..467be20 --- /dev/null +++ b/modules/spring/src/test/java/org/apache/ignite/transactions/spring/SpringTransactionManagerContextInjectionTest.java @@ -0,0 +1,125 @@ +/* + * 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.transactions.spring; + +import org.apache.ignite.Ignite; +import org.apache.ignite.TestInjectionLifecycleBean; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.lifecycle.LifecycleBean; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * + */ +public class SpringTransactionManagerContextInjectionTest extends GridCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testBeanInjectionUsingConfigPath() throws Exception { + BeanFactory factory = new AnnotationConfigApplicationContext(TestPathConfiguration.class); + + Ignite grid = IgnitionEx.grid("springInjectionTest"); + + IgniteConfiguration cfg = grid.configuration(); + + LifecycleBean[] beans = cfg.getLifecycleBeans(); + + assertEquals(2, beans.length); + + TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean)beans[0]; + TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean)beans[1]; + + bean1.checkState(); + bean2.checkState(); + } + + /** + * @throws Exception If failed. + */ + public void testBeanInjectionUsingConfig() throws Exception { + BeanFactory factory = new AnnotationConfigApplicationContext(TestCfgConfiguration.class); + + TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean)factory.getBean("bean1"); + TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean)factory.getBean("bean2"); + + bean1.checkState(); + bean2.checkState(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** */ + @SuppressWarnings("WeakerAccess") + @Configuration + static class TestPathConfiguration { + /** */ + @Bean(name = "mgr") + public SpringTransactionManager springTransactionManager() { + SpringTransactionManager mgr = new SpringTransactionManager(); + + mgr.setConfigurationPath("org/apache/ignite/spring-injection-test.xml"); + + return mgr; + } + } + + /** */ + @SuppressWarnings("WeakerAccess") + @Configuration + static class TestCfgConfiguration { + /** */ + @Bean(name = "mgr") + public SpringTransactionManager springTransactionManager() { + IgniteConfiguration cfg = new IgniteConfiguration(); + + cfg.setLocalHost("127.0.0.1"); + + cfg.setGridName("stmcit"); + + cfg.setLifecycleBeans(bean1(), bean2()); + + SpringTransactionManager mgr = new SpringTransactionManager(); + + mgr.setConfiguration(cfg); + + return mgr; + } + + /** */ + @Bean(name = "bean1") + LifecycleBean bean1() { + return new TestInjectionLifecycleBean(); + } + + /** */ + @Bean(name = "bean2") + LifecycleBean bean2() { + return new TestInjectionLifecycleBean(); + } + } +} \ No newline at end of file