|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.transaction.support;
|
18 | 18 |
|
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
19 | 22 | import org.junit.Test;
|
20 | 23 |
|
21 | 24 | import org.springframework.beans.factory.BeanCreationException;
|
22 | 25 | import org.springframework.beans.factory.support.GenericBeanDefinition;
|
23 | 26 | import org.springframework.context.support.GenericApplicationContext;
|
24 | 27 | import org.springframework.tests.sample.beans.DerivedTestBean;
|
25 | 28 | import org.springframework.tests.sample.beans.TestBean;
|
| 29 | +import org.springframework.tests.transaction.CallCountingTransactionManager; |
26 | 30 |
|
27 | 31 | import static org.junit.Assert.*;
|
28 | 32 |
|
@@ -93,7 +97,6 @@ public void getFromScope() throws Exception {
|
93 | 97 |
|
94 | 98 | bean2b = context.getBean(DerivedTestBean.class);
|
95 | 99 | assertSame(bean2b, context.getBean(DerivedTestBean.class));
|
96 |
| - assertNotSame(bean2, bean2a); |
97 | 100 | assertNotSame(bean2, bean2b);
|
98 | 101 | assertNotSame(bean2a, bean2b);
|
99 | 102 | }
|
@@ -125,4 +128,70 @@ public void getFromScope() throws Exception {
|
125 | 128 | }
|
126 | 129 | }
|
127 | 130 |
|
| 131 | + @Test |
| 132 | + public void getWithTransactionManager() throws Exception { |
| 133 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 134 | + context.getBeanFactory().registerScope("tx", new SimpleTransactionScope()); |
| 135 | + |
| 136 | + GenericBeanDefinition bd1 = new GenericBeanDefinition(); |
| 137 | + bd1.setBeanClass(TestBean.class); |
| 138 | + bd1.setScope("tx"); |
| 139 | + bd1.setPrimary(true); |
| 140 | + context.registerBeanDefinition("txScopedObject1", bd1); |
| 141 | + |
| 142 | + GenericBeanDefinition bd2 = new GenericBeanDefinition(); |
| 143 | + bd2.setBeanClass(DerivedTestBean.class); |
| 144 | + bd2.setScope("tx"); |
| 145 | + context.registerBeanDefinition("txScopedObject2", bd2); |
| 146 | + |
| 147 | + context.refresh(); |
| 148 | + |
| 149 | + CallCountingTransactionManager tm = new CallCountingTransactionManager(); |
| 150 | + TransactionTemplate tt = new TransactionTemplate(tm); |
| 151 | + Set<DerivedTestBean> finallyDestroy = new HashSet<DerivedTestBean>(); |
| 152 | + |
| 153 | + tt.execute(status -> { |
| 154 | + TestBean bean1 = context.getBean(TestBean.class); |
| 155 | + assertSame(bean1, context.getBean(TestBean.class)); |
| 156 | + |
| 157 | + DerivedTestBean bean2 = context.getBean(DerivedTestBean.class); |
| 158 | + assertSame(bean2, context.getBean(DerivedTestBean.class)); |
| 159 | + context.getBeanFactory().destroyScopedBean("txScopedObject2"); |
| 160 | + assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); |
| 161 | + assertTrue(bean2.wasDestroyed()); |
| 162 | + |
| 163 | + DerivedTestBean bean2a = context.getBean(DerivedTestBean.class); |
| 164 | + assertSame(bean2a, context.getBean(DerivedTestBean.class)); |
| 165 | + assertNotSame(bean2, bean2a); |
| 166 | + context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2"); |
| 167 | + assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); |
| 168 | + assertFalse(bean2a.wasDestroyed()); |
| 169 | + |
| 170 | + DerivedTestBean bean2b = context.getBean(DerivedTestBean.class); |
| 171 | + finallyDestroy.add(bean2b); |
| 172 | + assertSame(bean2b, context.getBean(DerivedTestBean.class)); |
| 173 | + assertNotSame(bean2, bean2b); |
| 174 | + assertNotSame(bean2a, bean2b); |
| 175 | + |
| 176 | + Set<DerivedTestBean> immediatelyDestroy = new HashSet<DerivedTestBean>(); |
| 177 | + TransactionTemplate tt2 = new TransactionTemplate(tm); |
| 178 | + tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED); |
| 179 | + tt2.execute(status2 -> { |
| 180 | + DerivedTestBean bean2c = context.getBean(DerivedTestBean.class); |
| 181 | + immediatelyDestroy.add(bean2c); |
| 182 | + assertSame(bean2c, context.getBean(DerivedTestBean.class)); |
| 183 | + assertNotSame(bean2, bean2c); |
| 184 | + assertNotSame(bean2a, bean2c); |
| 185 | + assertNotSame(bean2b, bean2c); |
| 186 | + return null; |
| 187 | + }); |
| 188 | + assertTrue(immediatelyDestroy.iterator().next().wasDestroyed()); |
| 189 | + assertFalse(bean2b.wasDestroyed()); |
| 190 | + |
| 191 | + return null; |
| 192 | + }); |
| 193 | + |
| 194 | + assertTrue(finallyDestroy.iterator().next().wasDestroyed()); |
| 195 | + } |
| 196 | + |
128 | 197 | }
|
0 commit comments