Skip to content

Commit 2934256

Browse files
committed
Polishing
1 parent 09eb492 commit 2934256

File tree

5 files changed

+119
-112
lines changed

5 files changed

+119
-112
lines changed

spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.core.task.AsyncTaskExecutor;
3737
import org.springframework.core.task.support.TaskExecutorAdapter;
3838
import org.springframework.lang.UsesJava8;
39-
import org.springframework.util.Assert;
4039
import org.springframework.util.ClassUtils;
4140
import org.springframework.util.ReflectionUtils;
4241
import org.springframework.util.StringUtils;
@@ -137,8 +136,10 @@ protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
137136
Executor executorToUse = this.defaultExecutor;
138137
String qualifier = getExecutorQualifier(method);
139138
if (StringUtils.hasLength(qualifier)) {
140-
Assert.notNull(this.beanFactory, "BeanFactory must be set on " + getClass().getSimpleName() +
141-
" to access qualified executor '" + qualifier + "'");
139+
if (this.beanFactory == null) {
140+
throw new IllegalStateException("BeanFactory must be set on " + getClass().getSimpleName() +
141+
" to access qualified executor '" + qualifier + "'");
142+
}
142143
executorToUse = BeanFactoryAnnotationUtils.qualifiedBeanOfType(
143144
this.beanFactory, Executor.class, qualifier);
144145
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ protected Class<?> getTypeForFactoryMethod(String beanName, RootBeanDefinition m
685685
if (Modifier.isStatic(factoryMethod.getModifiers()) == isStatic &&
686686
factoryMethod.getName().equals(mbd.getFactoryMethodName()) &&
687687
factoryMethod.getParameterTypes().length >= minNrOfArgs) {
688-
// No declared type variables to inspect, so just process the standard return type.
688+
// Declared type variables to inspect?
689689
if (factoryMethod.getTypeParameters().length > 0) {
690690
try {
691691
// Fully resolve parameter names and argument values.

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

Lines changed: 98 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,17 +67,6 @@ public void proxyingOccurs() {
6767
asyncBean.work();
6868
}
6969

70-
71-
@Configuration
72-
@EnableAsync
73-
static class AsyncConfig {
74-
@Bean
75-
public AsyncBean asyncBean() {
76-
return new AsyncBean();
77-
}
78-
}
79-
80-
8170
@Test
8271
public void withAsyncBeanWithExecutorQualifiedByName() throws ExecutionException, InterruptedException {
8372
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
@@ -95,8 +84,80 @@ public void withAsyncBeanWithExecutorQualifiedByName() throws ExecutionException
9584
assertThat(workerThread3.get().getName(), startsWith("otherExecutor-"));
9685
}
9786

87+
@Test
88+
public void asyncProcessorIsOrderedLowestPrecedenceByDefault() {
89+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
90+
ctx.register(AsyncConfig.class);
91+
ctx.refresh();
92+
93+
AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
94+
assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE));
95+
}
96+
97+
@Test
98+
public void orderAttributeIsPropagated() {
99+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
100+
ctx.register(OrderedAsyncConfig.class);
101+
ctx.refresh();
102+
103+
AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
104+
assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE));
105+
}
106+
107+
@Test
108+
public void customAsyncAnnotationIsPropagated() {
109+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
110+
ctx.register(CustomAsyncAnnotationConfig.class);
111+
ctx.refresh();
112+
113+
Object bean = ctx.getBean(CustomAsyncBean.class);
114+
assertTrue(AopUtils.isAopProxy(bean));
115+
boolean isAsyncAdvised = false;
116+
for (Advisor advisor : ((Advised)bean).getAdvisors()) {
117+
if (advisor instanceof AsyncAnnotationAdvisor) {
118+
isAsyncAdvised = true;
119+
break;
120+
}
121+
}
122+
assertTrue("bean was not async advised as expected", isAsyncAdvised);
123+
}
124+
125+
/**
126+
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
127+
*/
128+
@Test(expected = BeanDefinitionStoreException.class)
129+
public void aspectModeAspectJAttemptsToRegisterAsyncAspect() {
130+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
131+
ctx.register(AspectJAsyncAnnotationConfig.class);
132+
ctx.refresh();
133+
}
134+
135+
@Test
136+
public void customExecutorIsPropagated() throws InterruptedException {
137+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
138+
ctx.register(CustomExecutorAsyncConfig.class);
139+
ctx.refresh();
140+
141+
AsyncBean asyncBean = ctx.getBean(AsyncBean.class);
142+
asyncBean.work();
143+
Thread.sleep(500);
144+
assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-"));
145+
146+
TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler)
147+
ctx.getBean("exceptionHandler");
148+
assertFalse("handler should not have been called yet", exceptionHandler.isCalled());
149+
150+
asyncBean.fail();
151+
Thread.sleep(500);
152+
Method m = ReflectionUtils.findMethod(AsyncBean.class, "fail");
153+
exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
154+
155+
ctx.close();
156+
}
157+
98158

99159
static class AsyncBeanWithExecutorQualifiedByName {
160+
100161
@Async
101162
public Future<Thread> work0() {
102163
return new AsyncResult<Thread>(Thread.currentThread());
@@ -120,6 +181,7 @@ public Future<Thread> work3() {
120181

121182

122183
static class AsyncBean {
184+
123185
private Thread threadOfExecution;
124186

125187
@Async
@@ -138,60 +200,10 @@ public Thread getThreadOfExecution() {
138200
}
139201

140202

141-
@Test
142-
public void asyncProcessorIsOrderedLowestPrecedenceByDefault() {
143-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
144-
ctx.register(AsyncConfig.class);
145-
ctx.refresh();
146-
147-
AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
148-
assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE));
149-
}
150-
151-
152-
@Test
153-
public void orderAttributeIsPropagated() {
154-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
155-
ctx.register(OrderedAsyncConfig.class);
156-
ctx.refresh();
157-
158-
AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
159-
assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE));
160-
}
161-
162-
163-
@Configuration
164-
@EnableAsync(order=Ordered.HIGHEST_PRECEDENCE)
165-
static class OrderedAsyncConfig {
166-
@Bean
167-
public AsyncBean asyncBean() {
168-
return new AsyncBean();
169-
}
170-
}
171-
172-
173-
@Test
174-
public void customAsyncAnnotationIsPropagated() {
175-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
176-
ctx.register(CustomAsyncAnnotationConfig.class);
177-
ctx.refresh();
178-
179-
Object bean = ctx.getBean(CustomAsyncBean.class);
180-
assertTrue(AopUtils.isAopProxy(bean));
181-
boolean isAsyncAdvised = false;
182-
for (Advisor advisor : ((Advised)bean).getAdvisors()) {
183-
if (advisor instanceof AsyncAnnotationAdvisor) {
184-
isAsyncAdvised = true;
185-
break;
186-
}
187-
}
188-
assertTrue("bean was not async advised as expected", isAsyncAdvised);
189-
}
190-
191-
192203
@Configuration
193-
@EnableAsync(annotation=CustomAsync.class)
204+
@EnableAsync(annotation = CustomAsync.class)
194205
static class CustomAsyncAnnotationConfig {
206+
195207
@Bean
196208
public CustomAsyncBean asyncBean() {
197209
return new CustomAsyncBean();
@@ -206,25 +218,26 @@ public CustomAsyncBean asyncBean() {
206218

207219

208220
static class CustomAsyncBean {
221+
209222
@CustomAsync
210223
public void work() {
211224
}
212225
}
213226

214227

215-
/**
216-
* Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect
217-
*/
218-
@Test(expected=BeanDefinitionStoreException.class)
219-
public void aspectModeAspectJAttemptsToRegisterAsyncAspect() {
220-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
221-
ctx.register(AspectJAsyncAnnotationConfig.class);
222-
ctx.refresh();
228+
@Configuration
229+
@EnableAsync(order = Ordered.HIGHEST_PRECEDENCE)
230+
static class OrderedAsyncConfig {
231+
232+
@Bean
233+
public AsyncBean asyncBean() {
234+
return new AsyncBean();
235+
}
223236
}
224237

225238

226239
@Configuration
227-
@EnableAsync(mode=AdviceMode.ASPECTJ)
240+
@EnableAsync(mode = AdviceMode.ASPECTJ)
228241
static class AspectJAsyncAnnotationConfig {
229242
@Bean
230243
public AsyncBean asyncBean() {
@@ -233,33 +246,21 @@ public AsyncBean asyncBean() {
233246
}
234247

235248

236-
@Test
237-
public void customExecutorIsPropagated() throws InterruptedException {
238-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
239-
ctx.register(CustomExecutorAsyncConfig.class);
240-
ctx.refresh();
241-
242-
AsyncBean asyncBean = ctx.getBean(AsyncBean.class);
243-
asyncBean.work();
244-
Thread.sleep(500);
245-
assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-"));
246-
247-
TestableAsyncUncaughtExceptionHandler exceptionHandler = (TestableAsyncUncaughtExceptionHandler)
248-
ctx.getBean("exceptionHandler");
249-
assertFalse("handler should not have been called yet", exceptionHandler.isCalled());
250-
251-
asyncBean.fail();
252-
Thread.sleep(500);
253-
Method m = ReflectionUtils.findMethod(AsyncBean.class, "fail");
254-
exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
249+
@Configuration
250+
@EnableAsync
251+
static class AsyncConfig {
255252

256-
ctx.close();
253+
@Bean
254+
public AsyncBean asyncBean() {
255+
return new AsyncBean();
256+
}
257257
}
258258

259259

260260
@Configuration
261261
@EnableAsync
262262
static class CustomExecutorAsyncConfig implements AsyncConfigurer {
263+
263264
@Bean
264265
public AsyncBean asyncBean() {
265266
return new AsyncBean();
@@ -288,22 +289,22 @@ public AsyncUncaughtExceptionHandler exceptionHandler() {
288289
@Configuration
289290
@EnableAsync
290291
static class AsyncWithExecutorQualifiedByNameConfig {
292+
291293
@Bean
292294
public AsyncBeanWithExecutorQualifiedByName asyncBean() {
293295
return new AsyncBeanWithExecutorQualifiedByName();
294296
}
295297

296298
@Bean
297299
public Executor e1() {
298-
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
299-
return executor;
300+
return new ThreadPoolTaskExecutor();
300301
}
301302

302303
@Bean
303304
@Qualifier("e2")
304305
public Executor otherExecutor() {
305-
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
306-
return executor;
306+
return new ThreadPoolTaskExecutor();
307307
}
308308
}
309+
309310
}

spring-jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,8 +37,10 @@
3737
public class InitializeDatabaseIntegrationTests {
3838

3939
private String enabled;
40+
4041
private ClassPathXmlApplicationContext context;
4142

43+
4244
@Before
4345
public void init() {
4446
enabled = System.setProperty("ENABLED", "true");
@@ -48,14 +50,16 @@ public void init() {
4850
public void after() {
4951
if (enabled != null) {
5052
System.setProperty("ENABLED", enabled);
51-
} else {
53+
}
54+
else {
5255
System.clearProperty("ENABLED");
5356
}
5457
if (context != null) {
5558
context.close();
5659
}
5760
}
5861

62+
5963
@Test
6064
public void testCreateEmbeddedDatabase() throws Exception {
6165
context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-config.xml");
@@ -107,13 +111,15 @@ public void testCacheInitialization() throws Exception {
107111
}
108112

109113
private void assertCorrectSetup(DataSource dataSource) {
110-
JdbcTemplate t = new JdbcTemplate(dataSource);
111-
assertEquals(1, t.queryForObject("select count(*) from T_TEST", Integer.class).intValue());
114+
JdbcTemplate jt = new JdbcTemplate(dataSource);
115+
assertEquals(1, jt.queryForObject("select count(*) from T_TEST", Integer.class).intValue());
112116
}
113117

118+
114119
public static class CacheData implements InitializingBean {
115120

116121
private JdbcTemplate jdbcTemplate;
122+
117123
private List<Map<String,Object>> cache;
118124

119125
public void setDataSource(DataSource dataSource) {
@@ -128,7 +134,6 @@ public List<Map<String,Object>> getCachedData() {
128134
public void afterPropertiesSet() throws Exception {
129135
cache = jdbcTemplate.queryForList("SELECT * FROM T_TEST");
130136
}
131-
132137
}
133138

134139
}

0 commit comments

Comments
 (0)