11/*
2- * Copyright 2002-2011 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.
@@ -88,7 +88,7 @@ public class DelegatingFilterProxy extends GenericFilterBean {
8888
8989 private boolean targetFilterLifecycle = false ;
9090
91- private Filter delegate ;
91+ private volatile Filter delegate ;
9292
9393 private final Object delegateMonitor = new Object ();
9494
@@ -227,7 +227,6 @@ protected void initFilterBean() throws ServletException {
227227 if (this .targetBeanName == null ) {
228228 this .targetBeanName = getFilterName ();
229229 }
230-
231230 // Fetch Spring root application context and initialize the delegate early,
232231 // if possible. If the root application context will be started after this
233232 // filter proxy, we'll have to resort to lazy initialization.
@@ -243,16 +242,18 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
243242 throws ServletException , IOException {
244243
245244 // Lazily initialize the delegate if necessary.
246- Filter delegateToUse = null ;
247- synchronized (this .delegateMonitor ) {
248- if (this .delegate == null ) {
249- WebApplicationContext wac = findWebApplicationContext ();
250- if (wac == null ) {
251- throw new IllegalStateException ("No WebApplicationContext found: no ContextLoaderListener registered?" );
245+ Filter delegateToUse = this .delegate ;
246+ if (delegateToUse == null ) {
247+ synchronized (this .delegateMonitor ) {
248+ if (this .delegate == null ) {
249+ WebApplicationContext wac = findWebApplicationContext ();
250+ if (wac == null ) {
251+ throw new IllegalStateException ("No WebApplicationContext found: no ContextLoaderListener registered?" );
252+ }
253+ this .delegate = initDelegate (wac );
252254 }
253- this . delegate = initDelegate ( wac ) ;
255+ delegateToUse = this . delegate ;
254256 }
255- delegateToUse = this .delegate ;
256257 }
257258
258259 // Let the delegate perform the actual doFilter operation.
@@ -261,10 +262,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
261262
262263 @ Override
263264 public void destroy () {
264- Filter delegateToUse = null ;
265- synchronized (this .delegateMonitor ) {
266- delegateToUse = this .delegate ;
267- }
265+ Filter delegateToUse = this .delegate ;
268266 if (delegateToUse != null ) {
269267 destroyDelegate (delegateToUse );
270268 }
@@ -281,8 +279,7 @@ public void destroy() {
281279 * {@code ServletContext} before this filter gets initialized (or invoked).
282280 * <p>Subclasses may override this method to provide a different
283281 * {@code WebApplicationContext} retrieval strategy.
284- * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not
285- * found
282+ * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not found
286283 * @see #DelegatingFilterProxy(String, WebApplicationContext)
287284 * @see #getContextAttribute()
288285 * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext)
0 commit comments