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