Skip to content

Commit 354dad6

Browse files
committed
Avoid early initialization of empty interceptor names
Closes gh-12238
1 parent 0668323 commit 354dad6

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -421,11 +421,7 @@ private boolean isNamedBeanAnAdvisorOrAdvice(String beanName) {
421421
* are unaffected by such changes.
422422
*/
423423
private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
424-
if (this.advisorChainInitialized) {
425-
return;
426-
}
427-
428-
if (!ObjectUtils.isEmpty(this.interceptorNames)) {
424+
if (!this.advisorChainInitialized && !ObjectUtils.isEmpty(this.interceptorNames)) {
429425
if (this.beanFactory == null) {
430426
throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
431427
"- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames));
@@ -464,9 +460,9 @@ private synchronized void initializeAdvisorChain() throws AopConfigException, Be
464460
addAdvisorOnChainCreation(advice);
465461
}
466462
}
467-
}
468463

469-
this.advisorChainInitialized = true;
464+
this.advisorChainInitialized = true;
465+
}
470466
}
471467

472468

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,10 +65,10 @@
6565
import static org.assertj.core.api.Assertions.assertThatIOException;
6666

6767
/**
68-
* @since 13.03.2003
6968
* @author Rod Johnson
7069
* @author Juergen Hoeller
7170
* @author Chris Beams
71+
* @since 13.03.2003
7272
*/
7373
public class ProxyFactoryBeanTests {
7474

@@ -631,20 +631,50 @@ public void testFrozenFactoryBean() {
631631
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
632632
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(FROZEN_CONTEXT, CLASS));
633633

634-
Advised advised = (Advised)bf.getBean("frozen");
634+
Advised advised = (Advised) bf.getBean("frozen");
635635
assertThat(advised.isFrozen()).as("The proxy should be frozen").isTrue();
636636
}
637637

638638
@Test
639-
public void testDetectsInterfaces() throws Exception {
639+
public void testDetectsInterfaces() {
640640
ProxyFactoryBean fb = new ProxyFactoryBean();
641641
fb.setTarget(new TestBean());
642642
fb.addAdvice(new DebugInterceptor());
643643
fb.setBeanFactory(new DefaultListableBeanFactory());
644+
644645
ITestBean proxy = (ITestBean) fb.getObject();
645646
assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
646647
}
647648

649+
@Test
650+
public void testWithInterceptorNames() {
651+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
652+
bf.registerSingleton("debug", new DebugInterceptor());
653+
654+
ProxyFactoryBean fb = new ProxyFactoryBean();
655+
fb.setTarget(new TestBean());
656+
fb.setInterceptorNames("debug");
657+
fb.setBeanFactory(bf);
658+
659+
Advised proxy = (Advised) fb.getObject();
660+
assertThat(proxy.getAdvisors().length).isEqualTo(1);
661+
}
662+
663+
@Test
664+
public void testWithLateInterceptorNames() {
665+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
666+
bf.registerSingleton("debug", new DebugInterceptor());
667+
668+
ProxyFactoryBean fb = new ProxyFactoryBean();
669+
fb.setTarget(new TestBean());
670+
fb.setBeanFactory(bf);
671+
fb.getObject();
672+
673+
fb.setInterceptorNames("debug");
674+
Advised proxy = (Advised) fb.getObject();
675+
assertThat(proxy.getAdvisors().length).isEqualTo(1);
676+
}
677+
648678

649679
/**
650680
* Fires only on void methods. Saves list of methods intercepted.

0 commit comments

Comments
 (0)