Skip to content

Commit 6405551

Browse files
jhoellerunknown
authored andcommitted
Introduced assertions for bean accessor delegation in AbstractApplicationContext
Issue: SPR-10307
1 parent 30b21a9 commit 6405551

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,24 +1094,50 @@ public boolean isActive() {
10941094
}
10951095
}
10961096

1097+
/**
1098+
* Assert that this context's BeanFactory is currently active,
1099+
* throwing an {@link IllegalStateException} if it isn't.
1100+
* <p>Invoked by all {@link BeanFactory} delegation methods that depend
1101+
* on an active context, i.e. in particular all bean accessor methods.
1102+
* <p>The default implementation checks the {@link #isActive() 'active'} status
1103+
* of this context overall. May be overridden for more specific checks, or for a
1104+
* no-op if {@link #getBeanFactory()} itself throws an exception in such a case.
1105+
*/
1106+
protected void assertBeanFactoryActive() {
1107+
synchronized (this.activeMonitor) {
1108+
if (!this.active) {
1109+
if (this.closed) {
1110+
throw new IllegalStateException(getDisplayName() + " has been closed already");
1111+
}
1112+
else {
1113+
throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
1114+
}
1115+
}
1116+
}
1117+
}
1118+
10971119

10981120
//---------------------------------------------------------------------
10991121
// Implementation of BeanFactory interface
11001122
//---------------------------------------------------------------------
11011123

11021124
public Object getBean(String name) throws BeansException {
1125+
assertBeanFactoryActive();
11031126
return getBeanFactory().getBean(name);
11041127
}
11051128

11061129
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
1130+
assertBeanFactoryActive();
11071131
return getBeanFactory().getBean(name, requiredType);
11081132
}
11091133

11101134
public <T> T getBean(Class<T> requiredType) throws BeansException {
1135+
assertBeanFactoryActive();
11111136
return getBeanFactory().getBean(requiredType);
11121137
}
11131138

11141139
public Object getBean(String name, Object... args) throws BeansException {
1140+
assertBeanFactoryActive();
11151141
return getBeanFactory().getBean(name, args);
11161142
}
11171143

@@ -1120,18 +1146,22 @@ public boolean containsBean(String name) {
11201146
}
11211147

11221148
public boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
1149+
assertBeanFactoryActive();
11231150
return getBeanFactory().isSingleton(name);
11241151
}
11251152

11261153
public boolean isPrototype(String name) throws NoSuchBeanDefinitionException {
1154+
assertBeanFactoryActive();
11271155
return getBeanFactory().isPrototype(name);
11281156
}
11291157

11301158
public boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException {
1159+
assertBeanFactoryActive();
11311160
return getBeanFactory().isTypeMatch(name, targetType);
11321161
}
11331162

11341163
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
1164+
assertBeanFactoryActive();
11351165
return getBeanFactory().getType(name);
11361166
}
11371167

@@ -1157,30 +1187,36 @@ public String[] getBeanDefinitionNames() {
11571187
}
11581188

11591189
public String[] getBeanNamesForType(Class<?> type) {
1190+
assertBeanFactoryActive();
11601191
return getBeanFactory().getBeanNamesForType(type);
11611192
}
11621193

11631194
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
1195+
assertBeanFactoryActive();
11641196
return getBeanFactory().getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
11651197
}
11661198

11671199
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
1200+
assertBeanFactoryActive();
11681201
return getBeanFactory().getBeansOfType(type);
11691202
}
11701203

11711204
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
11721205
throws BeansException {
11731206

1207+
assertBeanFactoryActive();
11741208
return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
11751209
}
11761210

11771211
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
11781212
throws BeansException {
11791213

1214+
assertBeanFactoryActive();
11801215
return getBeanFactory().getBeansWithAnnotation(annotationType);
11811216
}
11821217

11831218
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
1219+
assertBeanFactoryActive();
11841220
return getBeanFactory().findAnnotationOnBean(beanName, annotationType);
11851221
}
11861222

spring-context/src/main/java/org/springframework/context/support/AbstractRefreshableApplicationContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -175,6 +175,13 @@ public final ConfigurableListableBeanFactory getBeanFactory() {
175175
}
176176
}
177177

178+
/**
179+
* Overridden to turn it into a no-op: With AbstractRefreshableApplicationContext,
180+
* {@link #getBeanFactory()} serves a strong assertion for an active context anyway.
181+
*/
182+
@Override
183+
protected void assertBeanFactoryActive() {
184+
}
178185

179186
/**
180187
* Create an internal bean factory for this context.

spring-context/src/main/java/org/springframework/context/support/StaticApplicationContext.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 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.
@@ -68,6 +68,13 @@ public StaticApplicationContext(ApplicationContext parent) throws BeansException
6868
}
6969

7070

71+
/**
72+
* Overridden to turn it into a no-op, to be more lenient towards test cases.
73+
*/
74+
@Override
75+
protected void assertBeanFactoryActive() {
76+
}
77+
7178
/**
7279
* Return the internal StaticMessageSource used by this context.
7380
* Can be used to register messages on it.
@@ -77,7 +84,6 @@ public final StaticMessageSource getStaticMessageSource() {
7784
return this.staticMessageSource;
7885
}
7986

80-
8187
/**
8288
* Register a singleton bean with the underlying bean factory.
8389
* <p>For more advanced needs, register with the underlying BeanFactory directly.

spring-webmvc/src/test/java/org/springframework/web/servlet/support/RequestContextTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -48,6 +48,7 @@ public class RequestContextTests {
4848
@Before
4949
public void init() {
5050
GenericWebApplicationContext applicationContext = new GenericWebApplicationContext();
51+
applicationContext.refresh();
5152
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
5253
}
5354

0 commit comments

Comments
 (0)