Skip to content

Commit c44447f

Browse files
committed
Avoid early initialization of empty interceptor names
Closes gh-12238
1 parent 8b80d38 commit c44447f

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

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

636-
Advised advised = (Advised)bf.getBean("frozen");
636+
Advised advised = (Advised) bf.getBean("frozen");
637637
assertThat(advised.isFrozen()).as("The proxy should be frozen").isTrue();
638638
}
639639

640640
@Test
641-
public void testDetectsInterfaces() throws Exception {
641+
public void testDetectsInterfaces() {
642642
ProxyFactoryBean fb = new ProxyFactoryBean();
643643
fb.setTarget(new TestBean());
644644
fb.addAdvice(new DebugInterceptor());
645645
fb.setBeanFactory(new DefaultListableBeanFactory());
646+
646647
ITestBean proxy = (ITestBean) fb.getObject();
647648
assertThat(AopUtils.isJdkDynamicProxy(proxy)).isTrue();
648649
}
649650

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

651681
/**
652682
* Fires only on void methods. Saves list of methods intercepted.

0 commit comments

Comments
 (0)