Spring loads its application context through the ContextLoaderListener
which is a ServletContextListener
(part of the Servlet API). All registered ServletContextListener
and other listener types are initialized before any declared Filter
s and Servlet
s.
As such, and assuming you set targetFilterLifecycle
to false
, by the time your DelegatingFilterProxy
is created by the Servlet container, your Filter
bean is already created and initialized inside the application context (that's where it should be declared).
The javadoc states the following about targetFilterLifecycle
:
Default is "false"; target beans usually rely on the Spring
application context for managing their lifecycle. Setting this flag to
"true" means that the servlet container will control the lifecycle of
the target Filter, with this proxy delegating the corresponding calls.
If you set it or leave it as false
, Spring will be responsible for initializing the object and doing any bean injection (and/or performing other lifecycle steps). If you set it to true
, Spring will give it to the Servlet container to do its own initialization after having done its own. This is done by invoking the init
method at startup and the destroy
method when shutting down.