001/*
002 * Copyright 2002-2012 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 org.aopalliance.aop.Advice;
020
021import org.springframework.aop.ClassFilter;
022import org.springframework.aop.Pointcut;
023import org.springframework.aop.support.AbstractPointcutAdvisor;
024
025/**
026 * Advisor driven by a {@link TransactionAttributeSource}, used to include
027 * a {@link TransactionInterceptor} only for methods that are transactional.
028 *
029 * <p>Because the AOP framework caches advice calculations, this is normally
030 * faster than just letting the TransactionInterceptor run and find out
031 * itself that it has no work to do.
032 *
033 * @author Rod Johnson
034 * @author Juergen Hoeller
035 * @see #setTransactionInterceptor
036 * @see TransactionProxyFactoryBean
037 */
038@SuppressWarnings("serial")
039public class TransactionAttributeSourceAdvisor extends AbstractPointcutAdvisor {
040
041        private TransactionInterceptor transactionInterceptor;
042
043        private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
044                @Override
045                protected TransactionAttributeSource getTransactionAttributeSource() {
046                        return (transactionInterceptor != null ? transactionInterceptor.getTransactionAttributeSource() : null);
047                }
048        };
049
050
051        /**
052         * Create a new TransactionAttributeSourceAdvisor.
053         */
054        public TransactionAttributeSourceAdvisor() {
055        }
056
057        /**
058         * Create a new TransactionAttributeSourceAdvisor.
059         * @param interceptor the transaction interceptor to use for this advisor
060         */
061        public TransactionAttributeSourceAdvisor(TransactionInterceptor interceptor) {
062                setTransactionInterceptor(interceptor);
063        }
064
065
066        /**
067         * Set the transaction interceptor to use for this advisor.
068         */
069        public void setTransactionInterceptor(TransactionInterceptor interceptor) {
070                this.transactionInterceptor = interceptor;
071        }
072
073        /**
074         * Set the {@link ClassFilter} to use for this pointcut.
075         * Default is {@link ClassFilter#TRUE}.
076         */
077        public void setClassFilter(ClassFilter classFilter) {
078                this.pointcut.setClassFilter(classFilter);
079        }
080
081
082        @Override
083        public Advice getAdvice() {
084                return this.transactionInterceptor;
085        }
086
087        @Override
088        public Pointcut getPointcut() {
089                return this.pointcut;
090        }
091
092}