Consistent overriding for all variants of init/destroy method inheritance

Issue: SPR-15532
This commit is contained in:
Juergen Hoeller
2017-08-18 00:13:49 +02:00
parent b94302b5bd
commit ac5e2599f7
5 changed files with 45 additions and 19 deletions

View File

@@ -1764,7 +1764,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (mbd != null) {
String initMethodName = mbd.getInitMethodName();
if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd);
}

View File

@@ -315,7 +315,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
setNonPublicAccessAllowed(otherAbd.isNonPublicAccessAllowed());
setLenientConstructorResolution(otherAbd.isLenientConstructorResolution());
getMethodOverrides().addOverrides(otherAbd.getMethodOverrides());
if (StringUtils.hasLength(otherAbd.getInitMethodName())) {
if (otherAbd.getInitMethodName() != null) {
setInitMethodName(otherAbd.getInitMethodName());
setEnforceInitMethod(otherAbd.isEnforceInitMethod());
}

View File

@@ -110,12 +110,6 @@ public class BeanDefinitionParserDelegate {
public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect";
public static final String DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all";
public static final String DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple";
public static final String DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects";
public static final String NAME_ATTRIBUTE = "name";
public static final String BEAN_ELEMENT = "bean";
@@ -609,9 +603,7 @@ public class BeanDefinitionParserDelegate {
if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
if (!"".equals(initMethodName)) {
bd.setInitMethodName(initMethodName);
}
bd.setInitMethodName(initMethodName);
}
else if (this.defaults.getInitMethod() != null) {
bd.setInitMethodName(this.defaults.getInitMethod());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -26,6 +26,7 @@ import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class DefaultLifecycleMethodsTests {
@@ -33,26 +34,31 @@ public class DefaultLifecycleMethodsTests {
@Before
public void setUp() throws Exception {
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(new ClassPathResource(
"defaultLifecycleMethods.xml", getClass()));
public void setup() throws Exception {
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("defaultLifecycleMethods.xml", getClass()));
}
@Test
public void lifecycleMethodsInvoked() {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleAware");
assertTrue("Bean not initialized", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
assertFalse("Bean destroyed too early", bean.isDestroyCalled());
this.beanFactory.destroySingletons();
assertTrue("Bean not destroyed", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
public void lifecycleMethodsDisabled() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleMethodsDisabled");
assertFalse("Bean init method called incorrectly", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
@@ -67,11 +73,32 @@ public class DefaultLifecycleMethodsTests {
@Test
public void overrideDefaultLifecycleMethods() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("overrideLifecycleMethods");
assertFalse("Default init method called incorrectly.", bean.isInitCalled());
assertTrue("Custom init method not called.", bean.isCustomInitCalled());
assertFalse("Default init method called incorrectly", bean.isInitCalled());
assertTrue("Custom init method not called", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Default destory method called incorrectly.", bean.isDestroyCalled());
assertTrue("Custom destory method not called.", bean.isCustomDestroyCalled());
assertFalse("Default destroy method called incorrectly", bean.isDestroyCalled());
assertTrue("Custom destroy method not called", bean.isCustomDestroyCalled());
}
@Test
public void childWithDefaultLifecycleMethods() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithDefaultLifecycleMethods");
assertTrue("Bean not initialized", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
assertFalse("Bean destroyed too early", bean.isDestroyCalled());
this.beanFactory.destroySingletons();
assertTrue("Bean not destroyed", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
public void childWithLifecycleMethodsDisabled() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithLifecycleMethodsDisabled");
assertFalse("Bean init method called incorrectly", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}

View File

@@ -11,4 +11,10 @@
<bean id="overrideLifecycleMethods" class="org.springframework.beans.factory.xml.DefaultLifecycleMethodsTests$LifecycleAwareBean"
init-method="customInit" destroy-method="customDestroy"/>
<bean id="childWithDefaultLifecycleMethods" class="org.springframework.beans.factory.xml.DefaultLifecycleMethodsTests$LifecycleAwareBean"
parent="overrideLifecycleMethods"/>
<bean id="childWithLifecycleMethodsDisabled" class="org.springframework.beans.factory.xml.DefaultLifecycleMethodsTests$LifecycleAwareBean"
parent="overrideLifecycleMethods" init-method="" destroy-method=""/>
</beans>