From 49ee4a4fdf54f90407cae6360c220032d4d7895b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 14 Nov 2022 23:35:47 +0100 Subject: [PATCH 1/2] Deprecated unused findAutowireCandidates template method (removed in 6.0) See gh-29487 --- .../annotation/AutowiredAnnotationBeanPostProcessor.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index f31d48191a4..77c3d66597f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -438,7 +438,6 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA } } - private InjectionMetadata findAutowiringMetadata(String beanName, Class clazz, @Nullable PropertyValues pvs) { // Fall back to class name as cache key, for backwards compatibility with custom callers. String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); @@ -563,7 +562,9 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA * @param type the type of the bean * @return the target beans, or an empty Collection if no bean of this type is found * @throws BeansException if bean retrieval failed + * @deprecated since 5.3.24 since it is unused in the meantime */ + @Deprecated protected Map findAutowireCandidates(Class type) throws BeansException { if (this.beanFactory == null) { throw new IllegalStateException("No BeanFactory configured - " + From ec3f59e6fea2b968ae00f41511c2e8ed6bda94d8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 14 Nov 2022 23:37:05 +0100 Subject: [PATCH 2/2] Allow AutoCloseable dereferences on original AutoCloseable beans Closes gh-29480 --- .../beans/CachedIntrospectionResults.java | 16 +++++++++------- .../springframework/beans/BeanWrapperTests.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java b/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java index 73165cc0cb8..42e9acca27f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java +++ b/spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java @@ -295,7 +295,7 @@ public final class CachedIntrospectionResults { // Only allow URL attribute introspection, not content resolution continue; } - if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) { + if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType(), beanClass)) { // Ignore read-only properties such as ClassLoader - no need to bind to those continue; } @@ -345,7 +345,8 @@ public final class CachedIntrospectionResults { // GenericTypeAwarePropertyDescriptor leniently resolves a set* write method // against a declared read method, so we prefer read method descriptors here. pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); - if (pd.getWriteMethod() == null && isInvalidReadOnlyPropertyType(pd.getPropertyType())) { + if (pd.getWriteMethod() == null && + isInvalidReadOnlyPropertyType(pd.getPropertyType(), beanClass)) { // Ignore read-only properties such as ClassLoader - no need to bind to those continue; } @@ -378,7 +379,7 @@ public final class CachedIntrospectionResults { if (Modifier.isStatic(method.getModifiers()) || method.getDeclaringClass() == Object.class || method.getDeclaringClass() == Class.class || method.getParameterCount() > 0 || method.getReturnType() == void.class || - isInvalidReadOnlyPropertyType(method.getReturnType())) { + isInvalidReadOnlyPropertyType(method.getReturnType(), method.getDeclaringClass())) { return false; } try { @@ -391,10 +392,11 @@ public final class CachedIntrospectionResults { } } - private boolean isInvalidReadOnlyPropertyType(@Nullable Class returnType) { - return (returnType != null && (AutoCloseable.class.isAssignableFrom(returnType) || - ClassLoader.class.isAssignableFrom(returnType) || - ProtectionDomain.class.isAssignableFrom(returnType))); + private boolean isInvalidReadOnlyPropertyType(@Nullable Class returnType, Class beanClass) { + return (returnType != null && (ClassLoader.class.isAssignableFrom(returnType) || + ProtectionDomain.class.isAssignableFrom(returnType) || + (AutoCloseable.class.isAssignableFrom(returnType) && + !AutoCloseable.class.isAssignableFrom(beanClass)))); } diff --git a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java index 30ab8b1716d..17a775a7f12 100644 --- a/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/BeanWrapperTests.java @@ -216,6 +216,10 @@ class BeanWrapperTests extends AbstractPropertyAccessorTests { assertThat(accessor.isReadableProperty("inputStream")).isFalse(); assertThat(accessor.isReadableProperty("filename")).isTrue(); assertThat(accessor.isReadableProperty("description")).isTrue(); + + accessor = createAccessor(new ActiveResource()); + + assertThat(accessor.isReadableProperty("resource")).isTrue(); } @Test @@ -376,6 +380,18 @@ class BeanWrapperTests extends AbstractPropertyAccessorTests { } + public static class ActiveResource implements AutoCloseable { + + public ActiveResource getResource() { + return this; + } + + @Override + public void close() throws Exception { + } + } + + public static class GetterWithOptional { public TestBean value;