001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.annotation;
018
019import java.lang.annotation.Annotation;
020
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.context.annotation.Bean;
023import org.springframework.context.annotation.Configuration;
024import org.springframework.context.annotation.Role;
025import org.springframework.core.annotation.AnnotationUtils;
026import org.springframework.scheduling.config.TaskManagementConfigUtils;
027import org.springframework.util.Assert;
028
029/**
030 * {@code @Configuration} class that registers the Spring infrastructure beans necessary
031 * to enable proxy-based asynchronous method execution.
032 *
033 * @author Chris Beams
034 * @author Stephane Nicoll
035 * @since 3.1
036 * @see EnableAsync
037 * @see AsyncConfigurationSelector
038 */
039@Configuration
040@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
041public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {
042
043        @Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
044        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
045        public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
046                Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
047                AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
048                Class<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");
049                if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) {
050                        bpp.setAsyncAnnotationType(customAsyncAnnotation);
051                }
052                if (this.executor != null) {
053                        bpp.setExecutor(this.executor);
054                }
055                if (this.exceptionHandler != null) {
056                        bpp.setExceptionHandler(this.exceptionHandler);
057                }
058                bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass"));
059                bpp.setOrder(this.enableAsync.<Integer>getNumber("order"));
060                return bpp;
061        }
062
063}