title: Spring Security needs ContextLoaderListener but I'm using annotated configuration, what to do?
body: <p>I was using annotated configuration for my Spring web app and then had to mix XML with it so that I could use Spring Security. I annotated one of my configuration classes with <code>@ImportResource("security-config.xml")</code> to load the security config. The configuration beans are being created just fine. My web.xml looks like so:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="com-timbuk2-webapp-compositor" 
           version="3.0"
           xmlns="http://java.sun.com/xml/ns/javaee" 
           xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                               http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" &gt;
  
      &lt;!-- Spring Security Chain --&gt;
      &lt;filter&gt;
           &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
           &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
         &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
         &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
       &lt;!--  Character Encoding --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;!--  URL Rewrite --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;urlRewriteFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;logLevel&lt;/param-name&gt;
              &lt;param-value&gt;commons&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;confPath&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/conf/urlrewrite-config.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;urlRewriteFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;!-- Listeners --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Context Parameters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/conf/log4j-config.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Servlets --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;app-dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextClass&lt;/param-name&gt;
              &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;com.website.config&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Servlet mappings --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;app-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/app/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>From what I understand the springSecurityFilterChain needs ContextLoaderListener. However, because of how my app is configured, if I add:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>to my web.xml my app doesn't initialize. Is there a way to manually create a ContextLoaderListener in my annotated configuration?</p>