Skip to content

Commit cf3a25b

Browse files
committed
Merge branch '6.0.x'
2 parents 8457c76 + 6bdf7ad commit cf3a25b

File tree

7 files changed

+70
-41
lines changed

7 files changed

+70
-41
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,32 @@ public Object getObject() throws BeansException {
265265
* Return the type of the proxy. Will check the singleton instance if
266266
* already created, else fall back to the proxy interface (in case of just
267267
* a single one), the target bean type, or the TargetSource's target class.
268-
* @see org.springframework.aop.TargetSource#getTargetClass
268+
* @see org.springframework.aop.framework.AopProxy#getProxyClass
269269
*/
270270
@Override
271+
@Nullable
271272
public Class<?> getObjectType() {
272273
synchronized (this) {
273274
if (this.singletonInstance != null) {
274275
return this.singletonInstance.getClass();
275276
}
276277
}
277-
// This might be incomplete since it potentially misses introduced interfaces
278-
// from Advisors that will be lazily retrieved via setInterceptorNames.
279-
return createAopProxy().getProxyClass(this.proxyClassLoader);
278+
try {
279+
// This might be incomplete since it potentially misses introduced interfaces
280+
// from Advisors that will be lazily retrieved via setInterceptorNames.
281+
return createAopProxy().getProxyClass(this.proxyClassLoader);
282+
}
283+
catch (AopConfigException ex) {
284+
if (getTargetClass() == null) {
285+
if (logger.isDebugEnabled()) {
286+
logger.debug("Failed to determine early proxy class: " + ex.getMessage());
287+
}
288+
return null;
289+
}
290+
else {
291+
throw ex;
292+
}
293+
}
280294
}
281295

282296
@Override

spring-aop/src/main/java/org/springframework/aop/support/ClassFilters.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public int hashCode() {
136136
public String toString() {
137137
return getClass().getName() + ": " + Arrays.toString(this.filters);
138138
}
139-
140139
}
141140

142141

@@ -177,7 +176,6 @@ public int hashCode() {
177176
public String toString() {
178177
return getClass().getName() + ": " + Arrays.toString(this.filters);
179178
}
180-
181179
}
182180

183181

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class BeanDefinitionPropertyValueCodeGenerator {
7070

7171
BeanDefinitionPropertyValueCodeGenerator(GeneratedMethods generatedMethods,
7272
@Nullable BiFunction<Object, ResolvableType, CodeBlock> customValueGenerator) {
73+
7374
this.generatedMethods = generatedMethods;
7475
this.delegates = new ArrayList<>();
7576
if (customValueGenerator != null) {
@@ -145,7 +146,6 @@ private interface Delegate {
145146

146147
@Nullable
147148
CodeBlock generateCode(Object value, ResolvableType type);
148-
149149
}
150150

151151

@@ -165,7 +165,6 @@ private static class PrimitiveDelegate implements Delegate {
165165
'\\', "\\\\"
166166
);
167167

168-
169168
@Override
170169
@Nullable
171170
public CodeBlock generateCode(Object value, ResolvableType type) {
@@ -233,7 +232,6 @@ public CodeBlock generateCode(Object value, ResolvableType type) {
233232
}
234233
return null;
235234
}
236-
237235
}
238236

239237

@@ -323,8 +321,8 @@ public CollectionDelegate(Class<?> collectionType, CodeBlock emptyResult) {
323321
this.emptyResult = emptyResult;
324322
}
325323

326-
@Override
327324
@SuppressWarnings("unchecked")
325+
@Override
328326
@Nullable
329327
public CodeBlock generateCode(Object value, ResolvableType type) {
330328
if (this.collectionType.isInstance(value)) {

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ else if (javaxResourceType != null && bridgedMethod.isAnnotationPresent(javaxRes
445445
* @see #getResource
446446
* @see Lazy
447447
*/
448-
protected Object buildLazyResourceProxy(final LookupElement element, final @Nullable String requestingBeanName) {
448+
protected Object buildLazyResourceProxy(LookupElement element, @Nullable String requestingBeanName) {
449449
TargetSource ts = new TargetSource() {
450450
@Override
451451
public Class<?> getTargetClass() {
@@ -525,16 +525,16 @@ protected Object autowireResource(BeanFactory factory, LookupElement element, @N
525525
String name = element.name;
526526

527527
if (factory instanceof AutowireCapableBeanFactory autowireCapableBeanFactory) {
528-
DependencyDescriptor descriptor = element.getDependencyDescriptor();
529528
if (this.fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) {
530529
autowiredBeanNames = new LinkedHashSet<>();
531-
resource = autowireCapableBeanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null);
530+
resource = autowireCapableBeanFactory.resolveDependency(
531+
element.getDependencyDescriptor(), requestingBeanName, autowiredBeanNames, null);
532532
if (resource == null) {
533533
throw new NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object");
534534
}
535535
}
536536
else {
537-
resource = autowireCapableBeanFactory.resolveBeanByName(name, descriptor);
537+
resource = autowireCapableBeanFactory.resolveBeanByName(name, element.getDependencyDescriptor());
538538
autowiredBeanNames = Collections.singleton(name);
539539
}
540540
}
@@ -661,8 +661,6 @@ protected Object getResourceToInject(Object target, @Nullable String requestingB
661661
}
662662

663663

664-
665-
666664
/**
667665
* Class representing injection information about an annotated field
668666
* or setter method, supporting the @Resource annotation.

spring-context/src/test/java/org/springframework/aop/framework/ProxyFactoryBeanTests.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,24 @@ public void testDoubleTargetSourcesAreRejected() {
139139
private void testDoubleTargetSourceIsRejected(String name) {
140140
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
141141
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(DBL_TARGETSOURCE_CONTEXT, CLASS));
142+
142143
assertThatExceptionOfType(BeanCreationException.class).as("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property")
143-
.isThrownBy(() -> bf.getBean(name))
144-
.havingCause()
145-
.isInstanceOf(AopConfigException.class)
146-
.withMessageContaining("TargetSource");
144+
.isThrownBy(() -> bf.getBean(name))
145+
.havingCause()
146+
.isInstanceOf(AopConfigException.class)
147+
.withMessageContaining("TargetSource");
147148
}
148149

149150
@Test
150151
public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() {
151152
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
152153
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(NOTLAST_TARGETSOURCE_CONTEXT, CLASS));
154+
153155
assertThatExceptionOfType(BeanCreationException.class).as("TargetSource or non-advised object must be last in interceptorNames")
154-
.isThrownBy(() -> bf.getBean("targetSourceNotLast"))
155-
.havingCause()
156-
.isInstanceOf(AopConfigException.class)
157-
.withMessageContaining("interceptorNames");
156+
.isThrownBy(() -> bf.getBean("targetSourceNotLast"))
157+
.havingCause()
158+
.isInstanceOf(AopConfigException.class)
159+
.withMessageContaining("interceptorNames");
158160
}
159161

160162
@Test
@@ -171,7 +173,7 @@ public void testGetObjectTypeWithDirectTarget() {
171173
assertThat(cba.getCalls()).isEqualTo(1);
172174

173175
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget");
174-
assertThat(TestBean.class.isAssignableFrom(pfb.getObjectType())).as("Has correct object type").isTrue();
176+
assertThat(pfb.getObjectType()).isAssignableTo(TestBean.class);
175177
}
176178

177179
@Test
@@ -181,7 +183,7 @@ public void testGetObjectTypeWithTargetViaTargetSource() {
181183
ITestBean tb = (ITestBean) bf.getBean("viaTargetSource");
182184
assertThat(tb.getName()).isEqualTo("Adam");
183185
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&viaTargetSource");
184-
assertThat(TestBean.class.isAssignableFrom(pfb.getObjectType())).as("Has correct object type").isTrue();
186+
assertThat(pfb.getObjectType()).isAssignableTo(TestBean.class);
185187
}
186188

187189
@Test
@@ -190,11 +192,15 @@ public void testGetObjectTypeWithNoTargetOrTargetSource() {
190192
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
191193

192194
ITestBean tb = (ITestBean) bf.getBean("noTarget");
193-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
194-
tb.getName())
195-
.withMessage("getName");
195+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(tb::getName).withMessage("getName");
196196
FactoryBean<?> pfb = (ProxyFactoryBean) bf.getBean("&noTarget");
197-
assertThat(ITestBean.class.isAssignableFrom(pfb.getObjectType())).as("Has correct object type").isTrue();
197+
assertThat(pfb.getObjectType()).isAssignableTo(ITestBean.class);
198+
}
199+
200+
@Test
201+
public void testGetObjectTypeOnUninitializedFactoryBean() {
202+
ProxyFactoryBean pfb = new ProxyFactoryBean();
203+
assertThat(pfb.getObjectType()).isNull();
198204
}
199205

200206
/**
@@ -227,12 +233,12 @@ public void testSingletonInstancesAreEqual() {
227233

228234
@Test
229235
public void testPrototypeInstancesAreNotEqual() {
230-
assertThat(ITestBean.class.isAssignableFrom(factory.getType("prototype"))).as("Has correct object type").isTrue();
236+
assertThat(factory.getType("prototype")).isAssignableTo(ITestBean.class);
231237
ITestBean test2 = (ITestBean) factory.getBean("prototype");
232238
ITestBean test2_1 = (ITestBean) factory.getBean("prototype");
233239
assertThat(test2).as("Prototype instances !=").isNotSameAs(test2_1);
234240
assertThat(test2).as("Prototype instances equal").isEqualTo(test2_1);
235-
assertThat(ITestBean.class.isAssignableFrom(factory.getType("prototype"))).as("Has correct object type").isTrue();
241+
assertThat(factory.getType("prototype")).isAssignableTo(ITestBean.class);
236242
}
237243

238244
/**
@@ -291,13 +297,13 @@ public void testAutoInvoker() {
291297
@Test
292298
public void testCanGetFactoryReferenceAndManipulate() {
293299
ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1");
294-
assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
295-
assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
300+
assertThat(config.getObjectType()).isAssignableTo(ITestBean.class);
301+
assertThat(factory.getType("test1")).isAssignableTo(ITestBean.class);
296302
// Trigger lazy initialization.
297303
config.getObject();
298304
assertThat(config.getAdvisors().length).as("Have one advisors").isEqualTo(1);
299-
assertThat(ITestBean.class.isAssignableFrom(config.getObjectType())).as("Has correct object type").isTrue();
300-
assertThat(ITestBean.class.isAssignableFrom(factory.getType("test1"))).as("Has correct object type").isTrue();
305+
assertThat(config.getObjectType()).isAssignableTo(ITestBean.class);
306+
assertThat(factory.getType("test1")).isAssignableTo(ITestBean.class);
301307

302308
ITestBean tb = (ITestBean) factory.getBean("test1");
303309
// no exception

spring-context/src/test/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessorTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,25 @@ public void testLazyResolutionWithCglibProxy() {
543543
assertThat(tb.getName()).isEqualTo("notLazyAnymore");
544544
}
545545

546+
@Test
547+
public void testLazyResolutionWithFallbackTypeMatch() {
548+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
549+
bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
550+
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
551+
bpp.setBeanFactory(bf);
552+
bf.addBeanPostProcessor(bpp);
553+
554+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(LazyResourceCglibInjectionBean.class));
555+
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class));
556+
557+
LazyResourceCglibInjectionBean bean = (LazyResourceCglibInjectionBean) bf.getBean("annotatedBean");
558+
assertThat(bf.containsSingleton("tb")).isFalse();
559+
bean.testBean.setName("notLazyAnymore");
560+
assertThat(bf.containsSingleton("tb")).isTrue();
561+
TestBean tb = (TestBean) bf.getBean("tb");
562+
assertThat(tb.getName()).isEqualTo("notLazyAnymore");
563+
}
564+
546565

547566
public static class AnnotatedInitDestroyBean {
548567

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ private static <T> T instantiate(KFunction<T> kotlinConstructor, Map<KParameter,
498498

499499
/**
500500
* Strategy for resolving constructor arguments based on their type.
501-
*
502501
* @since 6.0
503502
* @see ArgumentResolver#of(Class, Object)
504503
* @see ArgumentResolver#ofSupplied(Class, Supplier)
@@ -595,21 +594,18 @@ static <T> ArgumentResolver ofSupplied(Class<T> type, Supplier<T> valueSupplier)
595594
*/
596595
static ArgumentResolver from(Function<Class<?>, Object> function) {
597596
return new ArgumentResolver() {
598-
599597
@SuppressWarnings("unchecked")
600598
@Override
601599
public <T> T resolve(Class<T> type) {
602600
return (T) function.apply(type);
603601
}
604-
605602
};
606603
}
607604
}
608605

609606

610607
/**
611608
* Strategy for handling a failure that occurs when instantiating a factory.
612-
*
613609
* @since 6.0
614610
* @see FailureHandler#throwing()
615611
* @see FailureHandler#logging(Log)
@@ -671,7 +667,7 @@ static FailureHandler logging(Log logger) {
671667
static FailureHandler handleMessage(BiConsumer<Supplier<String>, Throwable> messageHandler) {
672668
return (factoryType, factoryImplementationName, failure) -> {
673669
Supplier<String> messageSupplier = () -> "Unable to instantiate factory class [%s] for factory type [%s]"
674-
.formatted(factoryImplementationName, factoryType.getName());
670+
.formatted(factoryImplementationName, factoryType.getName());
675671
messageHandler.accept(messageSupplier, failure);
676672
};
677673
}

0 commit comments

Comments
 (0)