Skip to content

Commit 66735d0

Browse files
committed
Add exception-handler attribute support for AspectJ
Previously, the exception-handler attribute was not taken care of when task:annotation-driven is used in AspectJ mode. This commit provides the expected behavior. Issue: SPR-12619
1 parent fa8d202 commit 66735d0

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.aspectj;
18+
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import org.springframework.beans.DirectFieldAccessor;
24+
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.context.support.ClassPathXmlApplicationContext;
26+
import org.springframework.scheduling.config.TaskManagementConfigUtils;
27+
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author Stephane Nicoll
32+
*/
33+
public class AnnotationDrivenBeanDefinitionParserTests {
34+
35+
private ConfigurableApplicationContext context;
36+
37+
@Before
38+
public void setup() {
39+
this.context = new ClassPathXmlApplicationContext(
40+
"annotationDrivenContext.xml", AnnotationDrivenBeanDefinitionParserTests.class);
41+
}
42+
43+
@After
44+
public void after() {
45+
if (this.context != null) {
46+
this.context.close();
47+
}
48+
}
49+
50+
@Test
51+
public void asyncAspectRegistered() {
52+
assertTrue(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME));
53+
}
54+
55+
@Test
56+
public void asyncPostProcessorExecutorReference() {
57+
Object executor = context.getBean("testExecutor");
58+
Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME);
59+
assertSame(executor, new DirectFieldAccessor(aspect).getPropertyValue("defaultExecutor"));
60+
}
61+
62+
@Test
63+
public void asyncPostProcessorExceptionHandlerReference() {
64+
Object exceptionHandler = context.getBean("testExceptionHandler");
65+
Object aspect = context.getBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME);
66+
assertSame(exceptionHandler, new DirectFieldAccessor(aspect).getPropertyValue("exceptionHandler"));
67+
}
68+
69+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:task="http://www.springframework.org/schema/task"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/task
8+
http://www.springframework.org/schema/task/spring-task.xsd">
9+
10+
<task:annotation-driven mode="aspectj" executor="testExecutor"
11+
exception-handler="testExceptionHandler"/>
12+
13+
<task:executor id="testExecutor"/>
14+
15+
<bean id="testExceptionHandler"
16+
class="org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler"/>
17+
18+
</beans>

spring-context/src/main/java/org/springframework/scheduling/config/AnnotationDrivenBeanDefinitionParser.java

Lines changed: 5 additions & 1 deletion
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.
@@ -115,6 +115,10 @@ private void registerAsyncExecutionAspect(Element element, ParserContext parserC
115115
if (StringUtils.hasText(executor)) {
116116
builder.addPropertyReference("executor", executor);
117117
}
118+
String exceptionHandler = element.getAttribute("exception-handler");
119+
if (StringUtils.hasText(exceptionHandler)) {
120+
builder.addPropertyReference("exceptionHandler", exceptionHandler);
121+
}
118122
parserContext.registerBeanComponent(new BeanComponentDefinition(builder.getBeanDefinition(),
119123
TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME));
120124
}

spring-context/src/test/java/org/springframework/scheduling/config/AnnotationDrivenBeanDefinitionParserTests.java

Lines changed: 9 additions & 1 deletion
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.
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Mark Fisher
30+
* @author Stephane Nicoll
3031
*/
3132
public class AnnotationDrivenBeanDefinitionParserTests {
3233

@@ -64,4 +65,11 @@ public void scheduledPostProcessorSchedulerReference() {
6465
assertSame(scheduler, new DirectFieldAccessor(postProcessor).getPropertyValue("scheduler"));
6566
}
6667

68+
@Test
69+
public void asyncPostProcessorExceptionHandlerReference() {
70+
Object exceptionHandler = context.getBean("testExceptionHandler");
71+
Object postProcessor = context.getBean(TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
72+
assertSame(exceptionHandler, new DirectFieldAccessor(postProcessor).getPropertyValue("exceptionHandler"));
73+
}
74+
6775
}

spring-context/src/test/resources/org/springframework/scheduling/config/annotationDrivenContext.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
http://www.springframework.org/schema/task
88
http://www.springframework.org/schema/task/spring-task.xsd">
99

10-
<task:annotation-driven executor="testExecutor" scheduler="testScheduler"/>
10+
<task:annotation-driven executor="testExecutor" scheduler="testScheduler"
11+
exception-handler="testExceptionHandler"/>
1112

1213
<task:executor id="testExecutor"/>
1314

1415
<task:scheduler id="testScheduler"/>
1516

17+
<bean id="testExceptionHandler"
18+
class="org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler"/>
19+
1620
</beans>

0 commit comments

Comments
 (0)