Added support for the 'annotation-driven' element within the 'task' namespace to enable detection of the @Async annotation on Spring-managed objects.
This commit is contained in:
parent
12923f196a
commit
3192b926ea
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2009 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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.springframework.scheduling.config;
|
||||||
|
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||||
|
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||||
|
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parser for the 'annotation-driven' element of the 'task' namespace.
|
||||||
|
*
|
||||||
|
* @author Mark Fisher
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bean name of the internally managed async annotation processor.
|
||||||
|
*/
|
||||||
|
public static final String ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME =
|
||||||
|
"org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor";
|
||||||
|
|
||||||
|
|
||||||
|
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||||
|
Object source = parserContext.extractSource(element);
|
||||||
|
|
||||||
|
// Register component for the surrounding <task:annotation-driven> element.
|
||||||
|
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
|
||||||
|
parserContext.pushContainingComponent(compDefinition);
|
||||||
|
|
||||||
|
// Nest the concrete post-processor bean in the surrounding component.
|
||||||
|
BeanDefinitionRegistry registry = parserContext.getRegistry();
|
||||||
|
if (registry.containsBeanDefinition(ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)) {
|
||||||
|
parserContext.getReaderContext().error(
|
||||||
|
"Only one AsyncAnnotationBeanPostProcessor may exist within the context.", source);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||||
|
"org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor");
|
||||||
|
builder.getRawBeanDefinition().setSource(source);
|
||||||
|
String executor = element.getAttribute("executor");
|
||||||
|
if (StringUtils.hasText(executor)) {
|
||||||
|
builder.addPropertyReference("executor", executor);
|
||||||
|
}
|
||||||
|
BeanDefinitionHolder holder = registerPostProcessor(registry, builder, ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME);
|
||||||
|
parserContext.registerComponent(new BeanComponentDefinition(holder));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally register the composite component.
|
||||||
|
parserContext.popAndRegisterContainingComponent();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BeanDefinitionHolder registerPostProcessor(
|
||||||
|
BeanDefinitionRegistry registry, BeanDefinitionBuilder builder, String beanName) {
|
||||||
|
|
||||||
|
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||||
|
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
|
||||||
|
return new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -27,6 +27,7 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||||
public class TaskNamespaceHandler extends NamespaceHandlerSupport {
|
public class TaskNamespaceHandler extends NamespaceHandlerSupport {
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
|
this.registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
|
||||||
this.registerBeanDefinitionParser("scheduled-tasks", new ScheduledTasksBeanDefinitionParser());
|
this.registerBeanDefinitionParser("scheduled-tasks", new ScheduledTasksBeanDefinitionParser());
|
||||||
this.registerBeanDefinitionParser("scheduler", new SchedulerBeanDefinitionParser());
|
this.registerBeanDefinitionParser("scheduler", new SchedulerBeanDefinitionParser());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,26 @@
|
||||||
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
|
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
|
||||||
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
|
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
|
||||||
|
|
||||||
|
<xsd:element name="annotation-driven">
|
||||||
|
<xsd:annotation>
|
||||||
|
<xsd:documentation><![CDATA[
|
||||||
|
Enables the detection of @Async annotations on any Spring-managed object. If present,
|
||||||
|
a proxy will be generated for executing the annotated methods asynchronously.
|
||||||
|
]]></xsd:documentation>
|
||||||
|
</xsd:annotation>
|
||||||
|
<xsd:complexType>
|
||||||
|
<xsd:attribute name="executor" type="xsd:string" use="optional">
|
||||||
|
<xsd:annotation>
|
||||||
|
<xsd:documentation><![CDATA[
|
||||||
|
Specifies the java.util.Executor instance to use when invoking asynchronous methods.
|
||||||
|
If not provided, an instance of org.springframework.core.task.SimpleAsyncTaskExecutor
|
||||||
|
will be used by default
|
||||||
|
]]></xsd:documentation>
|
||||||
|
</xsd:annotation>
|
||||||
|
</xsd:attribute>
|
||||||
|
</xsd:complexType>
|
||||||
|
</xsd:element>
|
||||||
|
|
||||||
<xsd:element name="scheduler">
|
<xsd:element name="scheduler">
|
||||||
<xsd:annotation>
|
<xsd:annotation>
|
||||||
<xsd:documentation><![CDATA[
|
<xsd:documentation><![CDATA[
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue