From eb492ed5839fd9c4af98b88722e4c7715c5442cc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 18 Jan 2013 14:13:35 +0100 Subject: [PATCH] SpringBeanAutowiringInterceptor eagerly releases BeanFactory if post-construction fails Issue: SPR-10013 --- .../factory/access/BeanFactoryReference.java | 14 ++++------ .../SpringBeanAutowiringInterceptor.java | 28 +++++++++++-------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java index 953b0cacf47..c2c0a7c6294 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2013 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. @@ -16,7 +16,6 @@ package org.springframework.beans.factory.access; -import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.BeanFactory; /** @@ -34,7 +33,7 @@ public interface BeanFactoryReference { /** * Return the {@link BeanFactory} instance held by this reference. - * @throws IllegalStateException if invoked after release() has been called + * @throws IllegalStateException if invoked after {@code release()} has been called */ BeanFactory getFactory(); @@ -42,18 +41,17 @@ public interface BeanFactoryReference { * Indicate that the {@link BeanFactory} instance referred to by this object is not * needed any longer by the client code which obtained the {@link BeanFactoryReference}. *

Depending on the actual implementation of {@link BeanFactoryLocator}, and - * the actual type of BeanFactory, this may possibly not actually - * do anything; alternately in the case of a 'closeable' BeanFactory + * the actual type of {@code BeanFactory}, this may possibly not actually + * do anything; alternately in the case of a 'closeable' {@code BeanFactory} * or derived class (such as {@link org.springframework.context.ApplicationContext}) * may 'close' it, or may 'close' it once no more references remain. *

In an EJB usage scenario this would normally be called from - * ejbRemove() and ejbPassivate(). + * {@code ejbRemove()} and {@code ejbPassivate()}. *

This is safe to call multiple times. - * @throws FatalBeanException if the BeanFactory cannot be released * @see BeanFactoryLocator * @see org.springframework.context.access.ContextBeanFactoryReference * @see org.springframework.context.ConfigurableApplicationContext#close() */ - void release() throws FatalBeanException; + void release(); } diff --git a/org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java b/org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java index 251e688b840..33c15966306 100644 --- a/org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java +++ b/org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2013 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. @@ -34,13 +34,13 @@ import org.springframework.context.access.ContextSingletonBeanFactoryLocator; /** * EJB3-compliant interceptor class that injects Spring beans into - * fields and methods which are annotated with @Autowired. + * fields and methods which are annotated with {@code @Autowired}. * Performs injection after construction as well as after activation * of a passivated bean. * - *

To be applied through an @Interceptors annotation in + *

To be applied through an {@code @Interceptors} annotation in * the EJB Session Bean or Message-Driven Bean class, or through an - * interceptor-binding XML element in the EJB deployment descriptor. + * {@code interceptor-binding} XML element in the EJB deployment descriptor. * *

Delegates to Spring's {@link AutowiredAnnotationBeanPostProcessor} * underneath, allowing for customization of its specific settings through @@ -60,7 +60,7 @@ import org.springframework.context.access.ContextSingletonBeanFactoryLocator; * *

WARNING: Do not define the same bean as Spring-managed bean and as * EJB3 session bean in the same deployment unit. In particular, be - * careful when using the <context:component-scan> feature + * careful when using the {@code <context:component-scan>} feature * in combination with the deployment of Spring-based EJB3 session beans: * Make sure that the EJB3 session beans are not autodetected as * Spring-managed beans as well, using appropriate package restrictions. @@ -71,8 +71,6 @@ import org.springframework.context.access.ContextSingletonBeanFactoryLocator; * @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator * @see #getBeanFactoryLocatorKey - * @see org.springframework.ejb.support.AbstractEnterpriseBean#setBeanFactoryLocator - * @see org.springframework.ejb.support.AbstractEnterpriseBean#setBeanFactoryLocatorKey */ public class SpringBeanAutowiringInterceptor { @@ -99,9 +97,15 @@ public class SpringBeanAutowiringInterceptor { invocationContext.proceed(); } catch (RuntimeException ex) { + doReleaseBean(invocationContext.getTarget()); throw ex; } + catch (Error err) { + doReleaseBean(invocationContext.getTarget()); + throw err; + } catch (Exception ex) { + doReleaseBean(invocationContext.getTarget()); // Cannot declare a checked exception on WebSphere here - so we need to wrap. throw new EJBException(ex); } @@ -130,7 +134,7 @@ public class SpringBeanAutowiringInterceptor { /** * Determine the BeanFactory for autowiring the given target bean. * @param target the target bean to autowire - * @return the BeanFactory to use (never null) + * @return the BeanFactory to use (never {@code null}) * @see #getBeanFactoryReference */ protected BeanFactory getBeanFactory(Object target) { @@ -146,7 +150,7 @@ public class SpringBeanAutowiringInterceptor { *

The default implementation delegates to {@link #getBeanFactoryLocator} * and {@link #getBeanFactoryLocatorKey}. * @param target the target bean to autowire - * @return the BeanFactoryReference to use (never null) + * @return the BeanFactoryReference to use (never {@code null}) * @see #getBeanFactoryLocator * @see #getBeanFactoryLocatorKey * @see org.springframework.beans.factory.access.BeanFactoryLocator#useBeanFactory(String) @@ -163,7 +167,7 @@ public class SpringBeanAutowiringInterceptor { *

The default implementation exposes Spring's default * {@link ContextSingletonBeanFactoryLocator}. * @param target the target bean to autowire - * @return the BeanFactoryLocator to use (never null) + * @return the BeanFactoryLocator to use (never {@code null}) * @see org.springframework.context.access.ContextSingletonBeanFactoryLocator#getInstance() */ protected BeanFactoryLocator getBeanFactoryLocator(Object target) { @@ -174,11 +178,11 @@ public class SpringBeanAutowiringInterceptor { * Determine the BeanFactoryLocator key to use. This typically indicates * the bean name of the ApplicationContext definition in * classpath*:beanRefContext.xml resource files. - *

The default is null, indicating the single + *

The default is {@code null}, indicating the single * ApplicationContext defined in the locator. This must be overridden * if more than one shared ApplicationContext definition is available. * @param target the target bean to autowire - * @return the BeanFactoryLocator key to use (or null for + * @return the BeanFactoryLocator key to use (or {@code null} for * referring to the single ApplicationContext defined in the locator) */ protected String getBeanFactoryLocatorKey(Object target) {