Browse Source

SpringBeanAutowiringInterceptor eagerly releases BeanFactory if post-construction fails

Issue: SPR-10013
pull/1536/head
Juergen Hoeller 12 years ago
parent
commit
eb492ed583
  1. 14
      org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java
  2. 28
      org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java

14
org.springframework.beans/src/main/java/org/springframework/beans/factory/access/BeanFactoryReference.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 { @@ -34,7 +33,7 @@ public interface BeanFactoryReference {
/**
* Return the {@link BeanFactory} instance held by this reference.
* @throws IllegalStateException if invoked after <code>release()</code> has been called
* @throws IllegalStateException if invoked after {@code release()} has been called
*/
BeanFactory getFactory();
@ -42,18 +41,17 @@ public interface BeanFactoryReference { @@ -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}.
* <p>Depending on the actual implementation of {@link BeanFactoryLocator}, and
* the actual type of <code>BeanFactory</code>, this may possibly not actually
* do anything; alternately in the case of a 'closeable' <code>BeanFactory</code>
* 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.
* <p>In an EJB usage scenario this would normally be called from
* <code>ejbRemove()</code> and <code>ejbPassivate()</code>.
* {@code ejbRemove()} and {@code ejbPassivate()}.
* <p>This is safe to call multiple times.
* @throws FatalBeanException if the <code>BeanFactory</code> cannot be released
* @see BeanFactoryLocator
* @see org.springframework.context.access.ContextBeanFactoryReference
* @see org.springframework.context.ConfigurableApplicationContext#close()
*/
void release() throws FatalBeanException;
void release();
}

28
org.springframework.context/src/main/java/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 <code>@Autowired</code>.
* fields and methods which are annotated with {@code @Autowired}.
* Performs injection after construction as well as after activation
* of a passivated bean.
*
* <p>To be applied through an <code>@Interceptors</code> annotation in
* <p>To be applied through an {@code @Interceptors} annotation in
* the EJB Session Bean or Message-Driven Bean class, or through an
* <code>interceptor-binding</code> XML element in the EJB deployment descriptor.
* {@code interceptor-binding} XML element in the EJB deployment descriptor.
*
* <p>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; @@ -60,7 +60,7 @@ import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
*
* <p><b>WARNING: Do not define the same bean as Spring-managed bean and as
* EJB3 session bean in the same deployment unit.</b> In particular, be
* careful when using the <code>&lt;context:component-scan&gt;</code> feature
* careful when using the {@code &lt;context:component-scan&gt;} feature
* in combination with the deployment of Spring-based EJB3 session beans:
* Make sure that the EJB3 session beans are <i>not</i> autodetected as
* Spring-managed beans as well, using appropriate package restrictions.
@ -71,8 +71,6 @@ import org.springframework.context.access.ContextSingletonBeanFactoryLocator; @@ -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 { @@ -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 { @@ -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 <code>null</code>)
* @return the BeanFactory to use (never {@code null})
* @see #getBeanFactoryReference
*/
protected BeanFactory getBeanFactory(Object target) {
@ -146,7 +150,7 @@ public class SpringBeanAutowiringInterceptor { @@ -146,7 +150,7 @@ public class SpringBeanAutowiringInterceptor {
* <p>The default implementation delegates to {@link #getBeanFactoryLocator}
* and {@link #getBeanFactoryLocatorKey}.
* @param target the target bean to autowire
* @return the BeanFactoryReference to use (never <code>null</code>)
* @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 { @@ -163,7 +167,7 @@ public class SpringBeanAutowiringInterceptor {
* <p>The default implementation exposes Spring's default
* {@link ContextSingletonBeanFactoryLocator}.
* @param target the target bean to autowire
* @return the BeanFactoryLocator to use (never <code>null</code>)
* @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 { @@ -174,11 +178,11 @@ public class SpringBeanAutowiringInterceptor {
* Determine the BeanFactoryLocator key to use. This typically indicates
* the bean name of the ApplicationContext definition in
* <strong>classpath*:beanRefContext.xml</strong> resource files.
* <p>The default is <code>null</code>, indicating the single
* <p>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 <code>null</code> 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) {

Loading…
Cancel
Save