|
21 | 21 | import java.lang.annotation.RetentionPolicy;
|
22 | 22 | import java.lang.annotation.Target;
|
23 | 23 | import java.lang.reflect.Method;
|
24 |
| -import java.util.Properties; |
25 | 24 | import java.util.concurrent.ExecutionException;
|
26 | 25 | import java.util.concurrent.Executor;
|
27 | 26 | import java.util.concurrent.Executors;
|
|
42 | 41 | import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
43 | 42 | import org.springframework.beans.factory.annotation.Qualifier;
|
44 | 43 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
| 44 | +import org.springframework.context.ConfigurableApplicationContext; |
45 | 45 | import org.springframework.context.annotation.AdviceMode;
|
46 | 46 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
47 | 47 | import org.springframework.context.annotation.Bean;
|
48 | 48 | import org.springframework.context.annotation.Configuration;
|
49 | 49 | import org.springframework.context.annotation.Import;
|
50 | 50 | import org.springframework.context.annotation.Lazy;
|
51 |
| -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; |
52 | 51 | import org.springframework.core.Ordered;
|
53 | 52 | import org.springframework.lang.Nullable;
|
54 | 53 | import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
@@ -133,26 +132,25 @@ public void withAsyncBeanWithExecutorQualifiedByName() throws ExecutionException
|
133 | 132 | }
|
134 | 133 |
|
135 | 134 | @Test
|
136 |
| - public void withAsyncBeanWithExecutorQualifiedByExpression() throws ExecutionException, InterruptedException { |
137 |
| - AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
138 |
| - System.getProperties().put("myExecutor", "myExecutor1"); |
139 |
| - PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer(); |
140 |
| - placeholderConfigurer.setProperties(new Properties() {{ |
141 |
| - put("my.app.myExecutor", "myExecutor2"); |
142 |
| - }}); |
143 |
| - placeholderConfigurer.postProcessBeanFactory(context.getBeanFactory()); |
| 135 | + public void withAsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder() throws Exception { |
| 136 | + System.setProperty("myExecutor", "myExecutor1"); |
| 137 | + System.setProperty("my.app.myExecutor", "myExecutor2"); |
144 | 138 |
|
145 |
| - context.register(AsyncWithExecutorQualifiedByExpressionConfig.class); |
146 |
| - context.refresh(); |
| 139 | + Class<?> configClass = AsyncWithExecutorQualifiedByExpressionConfig.class; |
| 140 | + try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(configClass)) { |
| 141 | + AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder asyncBean = |
| 142 | + context.getBean(AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder.class); |
147 | 143 |
|
148 |
| - AsyncBeanWithExecutorQualifiedByExpression asyncBean = context.getBean(AsyncBeanWithExecutorQualifiedByExpression.class); |
149 |
| - Future<Thread> workerThread1 = asyncBean.myWork1(); |
150 |
| - assertThat(workerThread1.get().getName()).doesNotStartWith("myExecutor2-").startsWith("myExecutor1-"); |
| 144 | + Future<Thread> workerThread1 = asyncBean.myWork1(); |
| 145 | + assertThat(workerThread1.get().getName()).startsWith("myExecutor1-"); |
151 | 146 |
|
152 |
| - Future<Thread> workerThread2 = asyncBean.myWork2(); |
153 |
| - assertThat(workerThread2.get().getName()).startsWith("myExecutor2-"); |
154 |
| - |
155 |
| - context.close(); |
| 147 | + Future<Thread> workerThread2 = asyncBean.myWork2(); |
| 148 | + assertThat(workerThread2.get().getName()).startsWith("myExecutor2-"); |
| 149 | + } |
| 150 | + finally { |
| 151 | + System.clearProperty("myExecutor"); |
| 152 | + System.clearProperty("my.app.myExecutor"); |
| 153 | + } |
156 | 154 | }
|
157 | 155 |
|
158 | 156 | @Test
|
@@ -371,9 +369,9 @@ public Future<Thread> work3() {
|
371 | 369 | }
|
372 | 370 | }
|
373 | 371 |
|
374 |
| - static class AsyncBeanWithExecutorQualifiedByExpression { |
| 372 | + static class AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder { |
375 | 373 |
|
376 |
| - @Async("#{systemProperties.myExecutor}") |
| 374 | + @Async("#{environment['myExecutor']}") |
377 | 375 | public Future<Thread> myWork1() {
|
378 | 376 | return new AsyncResult<>(Thread.currentThread());
|
379 | 377 | }
|
@@ -518,8 +516,8 @@ public Executor otherExecutor() {
|
518 | 516 | static class AsyncWithExecutorQualifiedByExpressionConfig {
|
519 | 517 |
|
520 | 518 | @Bean
|
521 |
| - public AsyncBeanWithExecutorQualifiedByExpression asyncBean() { |
522 |
| - return new AsyncBeanWithExecutorQualifiedByExpression(); |
| 519 | + public AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder asyncBean() { |
| 520 | + return new AsyncBeanWithExecutorQualifiedByExpressionOrPlaceholder(); |
523 | 521 | }
|
524 | 522 |
|
525 | 523 | @Bean
|
|
0 commit comments