Skip to content

Commit 1ea54eb

Browse files
committed
GenericFilterBean lazily initializes its default environment
Alignment with HttpServletBean; consistent use of EnvironmentCapable. Issue: SPR-15469
1 parent 04f0f13 commit 1ea54eb

File tree

6 files changed

+191
-163
lines changed

6 files changed

+191
-163
lines changed

spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -103,18 +103,19 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
103103
void setParent(ApplicationContext parent);
104104

105105
/**
106-
* Return the Environment for this application context in configurable form.
106+
* Set the {@code Environment} for this application context.
107+
* @param environment the new environment
107108
* @since 3.1
108109
*/
109-
@Override
110-
ConfigurableEnvironment getEnvironment();
110+
void setEnvironment(ConfigurableEnvironment environment);
111111

112112
/**
113-
* Set the {@code Environment} for this application context.
114-
* @param environment the new environment
113+
* Return the {@code Environment} for this application context in configurable
114+
* form, allowing for further customization.
115115
* @since 3.1
116116
*/
117-
void setEnvironment(ConfigurableEnvironment environment);
117+
@Override
118+
ConfigurableEnvironment getEnvironment();
118119

119120
/**
120121
* Add a new BeanFactoryPostProcessor that will get applied to the internal

spring-context/src/main/java/org/springframework/context/EnvironmentAware.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -25,11 +25,12 @@
2525
*
2626
* @author Chris Beams
2727
* @since 3.1
28+
* @see org.springframework.core.env.EnvironmentCapable
2829
*/
2930
public interface EnvironmentAware extends Aware {
3031

3132
/**
32-
* Set the {@code Environment} that this object runs in.
33+
* Set the {@code Environment} that this component runs in.
3334
*/
3435
void setEnvironment(Environment environment);
3536

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,22 @@ public ApplicationContext getParent() {
280280
}
281281

282282
/**
283-
* {@inheritDoc}
284-
* <p>If {@code null}, a new environment will be initialized via
283+
* Set the {@code Environment} for this application context.
284+
* <p>Default value is determined by {@link #createEnvironment()}. Replacing the
285+
* default with this method is one option but configuration through {@link
286+
* #getEnvironment()} should also be considered. In either case, such modifications
287+
* should be performed <em>before</em> {@link #refresh()}.
288+
* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
289+
*/
290+
@Override
291+
public void setEnvironment(ConfigurableEnvironment environment) {
292+
this.environment = environment;
293+
}
294+
295+
/**
296+
* Return the {@code Environment} for this application context in configurable
297+
* form, allowing for further customization.
298+
* <p>If none specified, a default environment will be initialized via
285299
* {@link #createEnvironment()}.
286300
*/
287301
@Override
@@ -293,16 +307,12 @@ public ConfigurableEnvironment getEnvironment() {
293307
}
294308

295309
/**
296-
* {@inheritDoc}
297-
* <p>Default value is determined by {@link #createEnvironment()}. Replacing the
298-
* default with this method is one option but configuration through {@link
299-
* #getEnvironment()} should also be considered. In either case, such modifications
300-
* should be performed <em>before</em> {@link #refresh()}.
301-
* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
310+
* Create and return a new {@link StandardEnvironment}.
311+
* <p>Subclasses may override this method in order to supply
312+
* a custom {@link ConfigurableEnvironment} implementation.
302313
*/
303-
@Override
304-
public void setEnvironment(ConfigurableEnvironment environment) {
305-
this.environment = environment;
314+
protected ConfigurableEnvironment createEnvironment() {
315+
return new StandardEnvironment();
306316
}
307317

308318
/**
@@ -443,7 +453,7 @@ protected ResourcePatternResolver getResourcePatternResolver() {
443453
//---------------------------------------------------------------------
444454

445455
/**
446-
* {@inheritDoc}
456+
* Set the parent of this application context.
447457
* <p>The parent {@linkplain ApplicationContext#getEnvironment() environment} is
448458
* {@linkplain ConfigurableEnvironment#merge(ConfigurableEnvironment) merged} with
449459
* this (child) application context environment if the parent is non-{@code null} and
@@ -494,15 +504,6 @@ public Collection<ApplicationListener<?>> getApplicationListeners() {
494504
return this.applicationListeners;
495505
}
496506

497-
/**
498-
* Create and return a new {@link StandardEnvironment}.
499-
* <p>Subclasses may override this method in order to supply
500-
* a custom {@link ConfigurableEnvironment} implementation.
501-
*/
502-
protected ConfigurableEnvironment createEnvironment() {
503-
return new StandardEnvironment();
504-
}
505-
506507
@Override
507508
public void refresh() throws BeansException, IllegalStateException {
508509
synchronized (this.startupShutdownMonitor) {

spring-core/src/main/java/org/springframework/core/env/EnvironmentCapable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -41,7 +41,8 @@
4141
public interface EnvironmentCapable {
4242

4343
/**
44-
* Return the {@link Environment} associated with this component.
44+
* Return the {@link Environment} associated with this component
45+
* (may be {@code null} or a default environment).
4546
*/
4647
Environment getEnvironment();
4748

0 commit comments

Comments
 (0)