diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 366663514c..25c2533527 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -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); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java index 581e267391..782b299a1c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java @@ -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()); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java index 2dd1665b1c..357c67f3b0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java @@ -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()); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java index 61b56fa3ed..8271c61bfb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/xml/DefaultLifecycleMethodsTests.java @@ -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()); } diff --git a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml index 635df1adc8..7414f870b1 100644 --- a/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml +++ b/spring-beans/src/test/resources/org/springframework/beans/factory/xml/defaultLifecycleMethods.xml @@ -11,4 +11,10 @@ + + + +