title: Spring MVC Mixing xml and java @ContextConfiguration in integration test
body: <p>I am trying to configure a Spring MVC Integration test using a combination of XML config and <code>@Configuration</code> annotated classes. </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @TestPropertySource({"/spring-security.properties",
                       "/rabbitmq-default.properties",
                       "/mongodb-default.properties",
                       "/webapp-override.properties"})
  @ContextHierarchy({
      @ContextConfiguration("classpath:**/security-config.xml"),
      @ContextConfiguration(classes = RootConfig.class),
      @ContextConfiguration(classes = SpringMvcConfig.class)
  })
  public class BaseConfiguredMvcIntegrationTest {
  }
  </code></pre>
  
  <p>The java configurations are initialized correctly. The problem is although the <code>"**/security-config.xml"</code> file is found and parsed during initialization... all the spring security beans defined in there are never registered in the <code>WebApplicationContext</code>. </p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
  </code></pre>
  
  <p>So my question is how do you utilize both XML based and annotated based configuration in a Spring MVC Integration test? </p>
  
  <p>I could change the spring security config to java/annotated based one... I would rather not do this. I find using the spring security namespace more readable and concise than using the java config. </p>
  
  <p>Also, note this combined XML/Java configuration works perfectly fine in a non-test environment. </p>
  
  <ul>
  <li>Spring v4.1.6  </li>
  <li>Spring Security v4.0.1</li>
  </ul>
  
  <p><strong>WebApplicationContext Config:</strong></p>
  
  <pre><code>package com.gggdw.web.config;
  
  import javax.servlet.FilterRegistration;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRegistration;
  
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.WebApplicationInitializer;
  import org.springframework.web.context.ContextLoaderListener;
  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  import org.springframework.web.filter.DelegatingFilterProxy;
  import org.springframework.web.filter.HiddenHttpMethodFilter;
  import org.springframework.web.servlet.DispatcherServlet;
  
  
  @Configuration
  public class GGGWebInitializer implements WebApplicationInitializer {
  
      public static final String SERVLET_NAME = "ggg";
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          // Create the 'root' Spring application context
          AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(RootConfig.class);
  
          // Manage the lifecycle of the root application context
          servletContext.addListener(new ContextLoaderListener(rootContext));
  
          // Create the dispatcher servlet's Spring application context
          AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
          dispatcherContext.register(SpringMvcConfig.class);
  
          // Register and map the dispatcher servlet
          ServletRegistration.Dynamic dispatcher = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(dispatcherContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
  
          //Spring security config
          FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter(
                                                      "securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"));
  
          springSecurityFilterChain.addMappingForServletNames(null, false, SERVLET_NAME);
          //springSecurityFilterChain.setAsyncSupported(true);
  
          servletContext.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
  
      }
  
  }
  </code></pre>
  
  <p><strong>RootConfig.class</strong></p>
  
  <pre><code>@Configuration
  @Import({WebPropertiesConfig.class,                     // loads all properties files on class path from resources folder
           MongoConfig.class                              // init mongodb connection 
           })                     
  @ImportResource({"classpath:**/security-config.xml"})   // spring security xml config (java config not as readable)
  public class RootConfig {
  
  }
  </code></pre>
  
  <p><strong>security-config.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:security="http://www.springframework.org/schema/security"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;!-- &lt;context:property-placeholder location="classpath:spring-security.properties" /&gt; --&gt;
  
      &lt;security:global-method-security pre-post-annotations="enabled" secured-annotations="enabled"&gt;
         &lt;security:expression-handler ref="expressionHandler"/&gt;
      &lt;/security:global-method-security&gt;
  
      &lt;bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler"&gt;
          &lt;property name="permissionEvaluator" ref="permissionEvaluator"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="permissionEvaluator"
            class="com.my.local.package.security.GenericPermissionEvaluator"&gt;
      &lt;/bean&gt;
  
      &lt;!-- Configure Spring Security --&gt;
      &lt;security:http auto-config="true" use-expressions="true" &gt;
          &lt;security:form-login login-page="${spring.security.login-page}" 
                               login-processing-url="${spring.security.login-processing-url}" 
                               default-target-url="${spring.security.default-target-url}" 
                               authentication-failure-url="${spring.security.authentication-failure-url}" 
                               username-parameter="${spring.security.username-parameter}"
                               password-parameter="${spring.security.password-parameter}"
          /&gt;
          &lt;security:logout logout-url="${spring.security.logout-url}" 
                   logout-success-url="${spring.security.logout-success-url}" /&gt;
  
          &lt;security:intercept-url pattern="/**" requires-channel="https" /&gt;
          &lt;security:intercept-url pattern="/s/**" access="isAuthenticated()" requires-channel="https" /&gt;
          &lt;security:custom-filter ref="log4JMDCFilter" after="SECURITY_CONTEXT_FILTER"/&gt;
          &lt;security:access-denied-handler error-page="${spring.security.access-denied-handler-error-page}" /&gt;
          &lt;!-- &lt;security:session-management invalid-session-url="${spring.security.invalid-session-url}"/&gt; 
                2 types of invalid session, brand new user and a timeout of a previous logged in user
                both need to be handled differently --&gt;
      &lt;/security:http&gt;
  
      &lt;bean id="customUserDetailsService" class="com.my.local.package.CustomUserDetailsService" depends-on="userRepository"/&gt;   
  
      &lt;bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" /&gt;
  
  
     &lt;!--  log4j filter to add userName and ipAddress into logging on a per request/thread basis --&gt;
      &lt;bean id="log4JMDCFilter" class="com.my.local.package.filter.Log4JMDCFilter"/&gt;
  
      &lt;security:authentication-manager&gt;
         &lt;security:authentication-provider user-service-ref="customUserDetailsService"&gt;
             &lt;security:password-encoder ref="bCryptPasswordEncoder"/&gt;
         &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
  
  &lt;/beans&gt;
  </code></pre>