001/*
002 * Copyright 2002-2019 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.interceptor;
018
019import java.lang.reflect.Method;
020
021import org.springframework.lang.Nullable;
022
023/**
024 * Strategy interface used by {@link TransactionInterceptor} for metadata retrieval.
025 *
026 * <p>Implementations know how to source transaction attributes, whether from configuration,
027 * metadata attributes at source level (such as Java 5 annotations), or anywhere else.
028 *
029 * @author Rod Johnson
030 * @author Juergen Hoeller
031 * @since 15.04.2003
032 * @see TransactionInterceptor#setTransactionAttributeSource
033 * @see TransactionProxyFactoryBean#setTransactionAttributeSource
034 * @see org.springframework.transaction.annotation.AnnotationTransactionAttributeSource
035 */
036public interface TransactionAttributeSource {
037
038        /**
039         * Determine whether the given class is a candidate for transaction attributes
040         * in the metadata format of this {@code TransactionAttributeSource}.
041         * <p>If this method returns {@code false}, the methods on the given class
042         * will not get traversed for {@link #getTransactionAttribute} introspection.
043         * Returning {@code false} is therefore an optimization for non-affected
044         * classes, whereas {@code true} simply means that the class needs to get
045         * fully introspected for each method on the given class individually.
046         * @param targetClass the class to introspect
047         * @return {@code false} if the class is known to have no transaction
048         * attributes at class or method level; {@code true} otherwise. The default
049         * implementation returns {@code true}, leading to regular introspection.
050         * @since 5.2
051         */
052        default boolean isCandidateClass(Class<?> targetClass) {
053                return true;
054        }
055
056        /**
057         * Return the transaction attribute for the given method,
058         * or {@code null} if the method is non-transactional.
059         * @param method the method to introspect
060         * @param targetClass the target class (may be {@code null},
061         * in which case the declaring class of the method must be used)
062         * @return the matching transaction attribute, or {@code null} if none found
063         */
064        @Nullable
065        TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass);
066
067}