001/*
002 * Copyright 2002-2017 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.transaction.event;
018
019import java.lang.annotation.Documented;
020import java.lang.annotation.ElementType;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025import org.springframework.context.event.EventListener;
026import org.springframework.core.annotation.AliasFor;
027
028/**
029 * An {@link EventListener} that is invoked according to a {@link TransactionPhase}.
030 *
031 * <p>If the event is not published within the boundaries of a managed transaction, the
032 * event is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a
033 * transaction is running, the event is processed according to its {@code TransactionPhase}.
034 *
035 * <p>Adding {@link org.springframework.core.annotation.Order @Order} to your annotated
036 * method allows you to prioritize that listener amongst other listeners running before
037 * or after transaction completion.
038 *
039 * @author Stephane Nicoll
040 * @author Sam Brannen
041 * @since 4.2
042 */
043@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
044@Retention(RetentionPolicy.RUNTIME)
045@Documented
046@EventListener
047public @interface TransactionalEventListener {
048
049        /**
050         * Phase to bind the handling of an event to.
051         * <p>The default phase is {@link TransactionPhase#AFTER_COMMIT}.
052         * <p>If no transaction is in progress, the event is not processed at
053         * all unless {@link #fallbackExecution} has been enabled explicitly.
054         */
055        TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;
056
057        /**
058         * Whether the event should be processed if no transaction is running.
059         */
060        boolean fallbackExecution() default false;
061
062        /**
063         * Alias for {@link #classes}.
064         */
065        @AliasFor(annotation = EventListener.class, attribute = "classes")
066        Class<?>[] value() default {};
067
068        /**
069         * The event classes that this listener handles.
070         * <p>If this attribute is specified with a single value, the annotated
071         * method may optionally accept a single parameter. However, if this
072         * attribute is specified with multiple values, the annotated method
073         * must <em>not</em> declare any parameters.
074         */
075        @AliasFor(annotation = EventListener.class, attribute = "classes")
076        Class<?>[] classes() default {};
077
078        /**
079         * Spring Expression Language (SpEL) attribute used for making the event
080         * handling conditional.
081         * <p>The default is {@code ""}, meaning the event is always handled.
082         * @see EventListener#condition
083         */
084        String condition() default "";
085
086}