From e210f08dced17f01bf3308f399c8e089b8f8319a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 8 Jun 2023 17:25:46 +0200 Subject: [PATCH] Declare Advisor#isPerInstance() as default method Includes INSTANCE constants on default factory classes. Closes gh-30614 --- .../java/org/springframework/aop/Advisor.java | 7 +++++-- .../aop/aspectj/AspectJPointcutAdvisor.java | 7 +------ .../aop/aspectj/DeclareParentsAdvisor.java | 7 +------ .../framework/DefaultAdvisorChainFactory.java | 7 +++++++ .../aop/framework/DefaultAopProxyFactory.java | 8 +++++++- .../aop/framework/ProxyCreatorSupport.java | 4 ++-- .../aop/framework/ProxyFactoryBean.java | 7 +------ .../aop/support/AbstractPointcutAdvisor.java | 18 ++++-------------- .../support/DefaultIntroductionAdvisor.java | 17 ++++------------- .../StaticMethodMatcherPointcutAdvisor.java | 7 +------ 10 files changed, 33 insertions(+), 56 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/Advisor.java b/spring-aop/src/main/java/org/springframework/aop/Advisor.java index b10911aebf..100fbd0779 100644 --- a/spring-aop/src/main/java/org/springframework/aop/Advisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/Advisor.java @@ -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. @@ -62,8 +62,11 @@ public interface Advisor { * Typical Advisor implementations always return {@code true}. * Use singleton/prototype bean definitions or appropriate programmatic * proxy creation to ensure that Advisors have the correct lifecycle model. + *

As of 6.0.10, the default implementation returns {@code true}. * @return whether this advice is associated with a particular target instance */ - boolean isPerInstance(); + default boolean isPerInstance() { + return true; + } } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java index 442df0808f..fb1974faee 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJPointcutAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 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. @@ -67,11 +67,6 @@ public class AspectJPointcutAdvisor implements PointcutAdvisor, Ordered { } } - @Override - public boolean isPerInstance() { - return true; - } - @Override public Advice getAdvice() { return this.advice; diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java index 3a4f8992cb..43233666ad 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/DeclareParentsAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -91,11 +91,6 @@ public class DeclareParentsAdvisor implements IntroductionAdvisor { // Do nothing } - @Override - public boolean isPerInstance() { - return true; - } - @Override public Advice getAdvice() { return this.advice; diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java index 5bef4c501c..73c2fb4308 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java @@ -47,6 +47,13 @@ import org.springframework.lang.Nullable; @SuppressWarnings("serial") public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable { + /** + * Singleton instance of this class. + * @since 6.0.10 + */ + public static final DefaultAdvisorChainFactory INSTANCE = new DefaultAdvisorChainFactory(); + + @Override public List getInterceptorsAndDynamicInterceptionAdvice( Advised config, Method method, @Nullable Class targetClass) { diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java index 5710f8e03b..f97455dfc4 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -48,6 +48,12 @@ import org.springframework.util.ClassUtils; */ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable { + /** + * Singleton instance of this class. + * @since 6.0.10 + */ + public static final DefaultAopProxyFactory INSTANCE = new DefaultAopProxyFactory(); + private static final long serialVersionUID = 7930414337282325166L; diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java index 096294321f..a9b43befbc 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyCreatorSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -44,7 +44,7 @@ public class ProxyCreatorSupport extends AdvisedSupport { * Create a new ProxyCreatorSupport instance. */ public ProxyCreatorSupport() { - this.aopProxyFactory = new DefaultAopProxyFactory(); + this.aopProxyFactory = DefaultAopProxyFactory.INSTANCE; } /** diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java index 77b8858171..f963721c58 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -606,11 +606,6 @@ public class ProxyFactoryBean extends ProxyCreatorSupport throw new UnsupportedOperationException("Cannot invoke methods: " + this.message); } - @Override - public boolean isPerInstance() { - throw new UnsupportedOperationException("Cannot invoke methods: " + this.message); - } - @Override public String toString() { return this.message; diff --git a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java index 6937e31ce2..fc5527270e 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/AbstractPointcutAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -58,22 +58,12 @@ public abstract class AbstractPointcutAdvisor implements PointcutAdvisor, Ordere return Ordered.LOWEST_PRECEDENCE; } - @Override - public boolean isPerInstance() { - return true; - } - @Override public boolean equals(@Nullable Object other) { - if (this == other) { - return true; - } - if (!(other instanceof PointcutAdvisor otherAdvisor)) { - return false; - } - return (ObjectUtils.nullSafeEquals(getAdvice(), otherAdvisor.getAdvice()) && - ObjectUtils.nullSafeEquals(getPointcut(), otherAdvisor.getPointcut())); + return (this == other || (other instanceof PointcutAdvisor otherAdvisor && + ObjectUtils.nullSafeEquals(getAdvice(), otherAdvisor.getAdvice()) && + ObjectUtils.nullSafeEquals(getPointcut(), otherAdvisor.getPointcut()))); } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java index 8b70d019fe..930255c62c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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. @@ -134,11 +134,6 @@ public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFil return this.advice; } - @Override - public boolean isPerInstance() { - return true; - } - @Override public ClassFilter getClassFilter() { return this; @@ -152,13 +147,9 @@ public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFil @Override public boolean equals(@Nullable Object other) { - if (this == other) { - return true; - } - if (!(other instanceof DefaultIntroductionAdvisor otherAdvisor)) { - return false; - } - return (this.advice.equals(otherAdvisor.advice) && this.interfaces.equals(otherAdvisor.interfaces)); + return (this == other || (other instanceof DefaultIntroductionAdvisor otherAdvisor && + this.advice.equals(otherAdvisor.advice) && + this.interfaces.equals(otherAdvisor.interfaces))); } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java index 38dceb3008..f5b3ccdeaa 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/StaticMethodMatcherPointcutAdvisor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -77,11 +77,6 @@ public abstract class StaticMethodMatcherPointcutAdvisor extends StaticMethodMat return this.advice; } - @Override - public boolean isPerInstance() { - return true; - } - @Override public Pointcut getPointcut() { return this;