title: Spring profiles: Repository bean cannot be autowired
body: <p>I'm having issues when trying to run a JUnit Test with some Spring profile I've created. Actually, profiles do work fine, except for a repository bean, which cannot be autowired.</p>
  
  <p>This is the fragment of code I use to configure persistence in spring-integration-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
     .... Some namespaces definitions
  
      &lt;beans profile="dev"&gt;
          &lt;jdbc:embedded-database id="dataSource" type="H2" /&gt;
  
          &lt;!-- Define the JPA transaction manager --&gt;
          &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
              &lt;constructor-arg ref="entityManagerFactory" /&gt;
          &lt;/bean&gt;
  
  
          &lt;bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="dataSource" ref="dataSource" /&gt;
              &lt;property name="jpaVendorAdapter" ref="vendorAdaptor" /&gt;
              &lt;property name="packagesToScan" value="guiatv.persistence.*" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="abstractVendorAdaptor" abstract="true"&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="database" value="H2" /&gt;
              &lt;property name="showSql" value="false" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="entityManager"
              class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
              &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
          &lt;/bean&gt;
  
          &lt;bean id="vendorAdaptor"
              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
              parent="abstractVendorAdaptor"&gt;
          &lt;/bean&gt;
      &lt;/beans&gt;
  
      ... Some other beans, some of them with the same "dev" profile
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>So I though it would be enough by wrapping the whole persistence configuration with a "beans profile" element.</p>
  
  <p>Now, from my JUnit test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = ApplicationTest.class)
  @ActiveProfiles("dev")
  
  public class ScheduleLoaderTests {
      @Autowired
      ScheduleLoader schedLoader;
  
      @Autowired
      ScheduleRepository shedRep;
  
      @Test
      public void someTest() { ... }
  }
  </code></pre>
  
  <p>And then I'm annotating my ApplicationTest class with:</p>
  
  <blockquote>
    <p>@ImportResource("classpath:/META-INF/spring/integration/spring-integration-context.xml")</p>
  </blockquote>
  
  <p>But when I run the test, I get the following exception:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [guiatv.persistence.repository.ScheduleRepository] 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)}
  </code></pre>
  
  <p>Note that in my test class, the ScheduleLoader is previously autowired,indeed that bean is also within dev profile. If I try to run the test without 
  <strong>@ActiveProfiles("dev")</strong> annotation, then the exception is thrown by <strong>ScheduleLoader failed autowiring</strong>, as expected (since it's only in dev profile).  </p>
  
  <p>So what's the problem with spring profiles and my persistence configuration?</p>
  
  <p>PD: I have ensured that my repository and entity class are within that </p>
  
  <blockquote>
    <p>guiatv.persistence.*</p>
  </blockquote>
  
  <p>package.</p>
  
  <p>By the way, I'm using Spring Boot, so I also tried to make it work by adding <strong>spring.profiles.active=dev</strong> on my application.properties file instead of writing 
  <strong>@ActiveProfiles("dev") annotation</strong>.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Also, if I try to remove every "beans profile" elements, so that every component is a child of the root "beans" element, everything is working fine, either with no @ActiveProfiles("dev") annotation or with @ActiveProfiles("default") annotation.</p>