Redesign inner Pointcut implementations as standalone classes

Avoids exposure of implicit Advisor instance state in Pointcut instance.

See gh-30621
This commit is contained in:
Juergen Hoeller
2023-06-08 17:28:52 +02:00
parent 045df81f14
commit 6931106c5e
7 changed files with 93 additions and 90 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ package org.springframework.transaction.interceptor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
import org.springframework.lang.Nullable;
/**
* Advisor driven by a {@link TransactionAttributeSource}, used to include
@@ -34,16 +33,7 @@ import org.springframework.lang.Nullable;
@SuppressWarnings("serial")
public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {
@Nullable
private TransactionAttributeSource transactionAttributeSource;
private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
@Override
@Nullable
protected TransactionAttributeSource getTransactionAttributeSource() {
return transactionAttributeSource;
}
};
private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut();
/**
@@ -53,7 +43,7 @@ public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFa
* @see TransactionInterceptor#setTransactionAttributeSource
*/
public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource) {
this.transactionAttributeSource = transactionAttributeSource;
this.pointcut.setTransactionAttributeSource(transactionAttributeSource);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,13 +43,7 @@ public class TransactionAttributeSourceAdvisor extends AbstractPointcutAdvisor {
@Nullable
private TransactionInterceptor transactionInterceptor;
private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
@Override
@Nullable
protected TransactionAttributeSource getTransactionAttributeSource() {
return (transactionInterceptor != null ? transactionInterceptor.getTransactionAttributeSource() : null);
}
};
private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut();
/**
@@ -71,7 +65,9 @@ public class TransactionAttributeSourceAdvisor extends AbstractPointcutAdvisor {
* Set the transaction interceptor to use for this advisor.
*/
public void setTransactionInterceptor(TransactionInterceptor interceptor) {
Assert.notNull(interceptor, "TransactionInterceptor must not be null");
this.transactionInterceptor = interceptor;
this.pointcut.setTransactionAttributeSource(interceptor.getTransactionAttributeSource());
}
/**

View File

@@ -34,28 +34,31 @@ import org.springframework.util.ObjectUtils;
* @since 2.5.5
*/
@SuppressWarnings("serial")
abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
class TransactionAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
protected TransactionAttributeSourcePointcut() {
@Nullable
private TransactionAttributeSource transactionAttributeSource;
public TransactionAttributeSourcePointcut() {
setClassFilter(new TransactionAttributeSourceClassFilter());
}
public void setTransactionAttributeSource(@Nullable TransactionAttributeSource transactionAttributeSource) {
this.transactionAttributeSource = transactionAttributeSource;
}
@Override
public boolean matches(Method method, Class<?> targetClass) {
TransactionAttributeSource tas = getTransactionAttributeSource();
return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
return (this.transactionAttributeSource == null ||
this.transactionAttributeSource.getTransactionAttribute(method, targetClass) != null);
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof TransactionAttributeSourcePointcut otherPc)) {
return false;
}
return ObjectUtils.nullSafeEquals(getTransactionAttributeSource(), otherPc.getTransactionAttributeSource());
return (this == other || (other instanceof TransactionAttributeSourcePointcut otherPc &&
ObjectUtils.nullSafeEquals(this.transactionAttributeSource, otherPc.transactionAttributeSource)));
}
@Override
@@ -65,18 +68,10 @@ abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPoi
@Override
public String toString() {
return getClass().getName() + ": " + getTransactionAttributeSource();
return getClass().getName() + ": " + this.transactionAttributeSource;
}
/**
* Obtain the underlying TransactionAttributeSource (may be {@code null}).
* To be implemented by subclasses.
*/
@Nullable
protected abstract TransactionAttributeSource getTransactionAttributeSource();
/**
* {@link ClassFilter} that delegates to {@link TransactionAttributeSource#isCandidateClass}
* for filtering classes whose methods are not worth searching to begin with.
@@ -90,8 +85,7 @@ abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPoi
PersistenceExceptionTranslator.class.isAssignableFrom(clazz)) {
return false;
}
TransactionAttributeSource tas = getTransactionAttributeSource();
return (tas == null || tas.isCandidateClass(clazz));
return (transactionAttributeSource == null || transactionAttributeSource.isCandidateClass(clazz));
}
}