Skip to content

Commit eb492ed

Browse files
committed
SpringBeanAutowiringInterceptor eagerly releases BeanFactory if post-construction fails
Issue: SPR-10013
1 parent cd8d72e commit eb492ed

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.beans.factory.access;
1818

19-
import org.springframework.beans.FatalBeanException;
2019
import org.springframework.beans.factory.BeanFactory;
2120

2221
/**
@@ -34,26 +33,25 @@ public interface BeanFactoryReference {
3433

3534
/**
3635
* Return the {@link BeanFactory} instance held by this reference.
37-
* @throws IllegalStateException if invoked after <code>release()</code> has been called
36+
* @throws IllegalStateException if invoked after {@code release()} has been called
3837
*/
3938
BeanFactory getFactory();
4039

4140
/**
4241
* Indicate that the {@link BeanFactory} instance referred to by this object is not
4342
* needed any longer by the client code which obtained the {@link BeanFactoryReference}.
4443
* <p>Depending on the actual implementation of {@link BeanFactoryLocator}, and
45-
* the actual type of <code>BeanFactory</code>, this may possibly not actually
46-
* do anything; alternately in the case of a 'closeable' <code>BeanFactory</code>
44+
* the actual type of {@code BeanFactory}, this may possibly not actually
45+
* do anything; alternately in the case of a 'closeable' {@code BeanFactory}
4746
* or derived class (such as {@link org.springframework.context.ApplicationContext})
4847
* may 'close' it, or may 'close' it once no more references remain.
4948
* <p>In an EJB usage scenario this would normally be called from
50-
* <code>ejbRemove()</code> and <code>ejbPassivate()</code>.
49+
* {@code ejbRemove()} and {@code ejbPassivate()}.
5150
* <p>This is safe to call multiple times.
52-
* @throws FatalBeanException if the <code>BeanFactory</code> cannot be released
5351
* @see BeanFactoryLocator
5452
* @see org.springframework.context.access.ContextBeanFactoryReference
5553
* @see org.springframework.context.ConfigurableApplicationContext#close()
5654
*/
57-
void release() throws FatalBeanException;
55+
void release();
5856

5957
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -34,13 +34,13 @@
3434

3535
/**
3636
* EJB3-compliant interceptor class that injects Spring beans into
37-
* fields and methods which are annotated with <code>@Autowired</code>.
37+
* fields and methods which are annotated with {@code @Autowired}.
3838
* Performs injection after construction as well as after activation
3939
* of a passivated bean.
4040
*
41-
* <p>To be applied through an <code>@Interceptors</code> annotation in
41+
* <p>To be applied through an {@code @Interceptors} annotation in
4242
* the EJB Session Bean or Message-Driven Bean class, or through an
43-
* <code>interceptor-binding</code> XML element in the EJB deployment descriptor.
43+
* {@code interceptor-binding} XML element in the EJB deployment descriptor.
4444
*
4545
* <p>Delegates to Spring's {@link AutowiredAnnotationBeanPostProcessor}
4646
* underneath, allowing for customization of its specific settings through
@@ -60,7 +60,7 @@
6060
*
6161
* <p><b>WARNING: Do not define the same bean as Spring-managed bean and as
6262
* EJB3 session bean in the same deployment unit.</b> In particular, be
63-
* careful when using the <code>&lt;context:component-scan&gt;</code> feature
63+
* careful when using the {@code &lt;context:component-scan&gt;} feature
6464
* in combination with the deployment of Spring-based EJB3 session beans:
6565
* Make sure that the EJB3 session beans are <i>not</i> autodetected as
6666
* Spring-managed beans as well, using appropriate package restrictions.
@@ -71,8 +71,6 @@
7171
* @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
7272
* @see org.springframework.context.access.ContextSingletonBeanFactoryLocator
7373
* @see #getBeanFactoryLocatorKey
74-
* @see org.springframework.ejb.support.AbstractEnterpriseBean#setBeanFactoryLocator
75-
* @see org.springframework.ejb.support.AbstractEnterpriseBean#setBeanFactoryLocatorKey
7674
*/
7775
public class SpringBeanAutowiringInterceptor {
7876

@@ -99,9 +97,15 @@ public void autowireBean(InvocationContext invocationContext) {
9997
invocationContext.proceed();
10098
}
10199
catch (RuntimeException ex) {
100+
doReleaseBean(invocationContext.getTarget());
102101
throw ex;
103102
}
103+
catch (Error err) {
104+
doReleaseBean(invocationContext.getTarget());
105+
throw err;
106+
}
104107
catch (Exception ex) {
108+
doReleaseBean(invocationContext.getTarget());
105109
// Cannot declare a checked exception on WebSphere here - so we need to wrap.
106110
throw new EJBException(ex);
107111
}
@@ -130,7 +134,7 @@ protected void configureBeanPostProcessor(AutowiredAnnotationBeanPostProcessor p
130134
/**
131135
* Determine the BeanFactory for autowiring the given target bean.
132136
* @param target the target bean to autowire
133-
* @return the BeanFactory to use (never <code>null</code>)
137+
* @return the BeanFactory to use (never {@code null})
134138
* @see #getBeanFactoryReference
135139
*/
136140
protected BeanFactory getBeanFactory(Object target) {
@@ -146,7 +150,7 @@ protected BeanFactory getBeanFactory(Object target) {
146150
* <p>The default implementation delegates to {@link #getBeanFactoryLocator}
147151
* and {@link #getBeanFactoryLocatorKey}.
148152
* @param target the target bean to autowire
149-
* @return the BeanFactoryReference to use (never <code>null</code>)
153+
* @return the BeanFactoryReference to use (never {@code null})
150154
* @see #getBeanFactoryLocator
151155
* @see #getBeanFactoryLocatorKey
152156
* @see org.springframework.beans.factory.access.BeanFactoryLocator#useBeanFactory(String)
@@ -163,7 +167,7 @@ protected BeanFactoryReference getBeanFactoryReference(Object target) {
163167
* <p>The default implementation exposes Spring's default
164168
* {@link ContextSingletonBeanFactoryLocator}.
165169
* @param target the target bean to autowire
166-
* @return the BeanFactoryLocator to use (never <code>null</code>)
170+
* @return the BeanFactoryLocator to use (never {@code null})
167171
* @see org.springframework.context.access.ContextSingletonBeanFactoryLocator#getInstance()
168172
*/
169173
protected BeanFactoryLocator getBeanFactoryLocator(Object target) {
@@ -174,11 +178,11 @@ protected BeanFactoryLocator getBeanFactoryLocator(Object target) {
174178
* Determine the BeanFactoryLocator key to use. This typically indicates
175179
* the bean name of the ApplicationContext definition in
176180
* <strong>classpath*:beanRefContext.xml</strong> resource files.
177-
* <p>The default is <code>null</code>, indicating the single
181+
* <p>The default is {@code null}, indicating the single
178182
* ApplicationContext defined in the locator. This must be overridden
179183
* if more than one shared ApplicationContext definition is available.
180184
* @param target the target bean to autowire
181-
* @return the BeanFactoryLocator key to use (or <code>null</code> for
185+
* @return the BeanFactoryLocator key to use (or {@code null} for
182186
* referring to the single ApplicationContext defined in the locator)
183187
*/
184188
protected String getBeanFactoryLocatorKey(Object target) {

0 commit comments

Comments
 (0)