title: What happens in Spring if I use the @ActiveProfiles annotation on a configuration class instead use it on the class that defines my beans?
body: <p>I am studying for the <strong>Spring Core</strong> certification and I have some doubts related to the use of <strong>profiles</strong> into <strong>JUnit tests</strong>.</p>
  
  <p>So I know that if I annote a class in the following way:</p>
  
  <pre><code>@Profile("stub")
  @Repository
  public class StubAccountRepository implements AccountRepository {
  
      private Logger logger = Logger.getLogger(StubAccountRepository.class);
  
      private Map&lt;String, Account&gt; accountsByCreditCard = new HashMap&lt;String, Account&gt;();
  
      /**
       * Creates a single test account with two beneficiaries. Also logs creation
       * so we know which repository we are using.
       */
      public StubAccountRepository() {
          logger.info("Creating " + getClass().getSimpleName());
          Account account = new Account("123456789", "Keith and Keri Donald");
          account.addBeneficiary("Annabelle", Percentage.valueOf("50%"));
          account.addBeneficiary("Corgan", Percentage.valueOf("50%"));
          accountsByCreditCard.put("1234123412341234", account);
      }
  
      public Account findByCreditCard(String creditCardNumber) {
          Account account = accountsByCreditCard.get(creditCardNumber);
          if (account == null) {
              throw new EmptyResultDataAccessException(1);
          }
          return account;
      }
  
      public void updateBeneficiaries(Account account) {
          // nothing to do, everything is in memory
      }
  }
  </code></pre>
  
  <p>I am declaring a <strong>service bean</strong> that belongs to the <strong>stub</strong> profile.</p>
  
  <p>So, if my <strong>test class</strong> is something like this:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes=TestInfrastructureConfig.class)
  @ActiveProfiles("stub")
  public class RewardNetworkTests {
      .....................................
      .....................................
      .....................................
  }
  </code></pre>
  
  <p>it means that it will be used the beans bean that belong to the <strong>stub</strong> profile and the bean that not have a profile. Is it right or am I missing something?</p>
  
  <p>What happens if instead use the <strong>@ActiveProfiles</strong> annotation on a class (whose instance will be a Spring bean) I use it on a <strong>Java Configuration Class</strong>?</p>
  
  <p>Something like it:</p>
  
  <pre><code>@Configuration
  @Profile("jdbc-dev")
  public class TestInfrastructureDevConfig {
  
      /**
       * Creates an in-memory "rewards" database populated 
       * with test data for fast testing
       */
      @Bean
      public DataSource dataSource(){
          return
              (new EmbeddedDatabaseBuilder())
              .addScript("classpath:rewards/testdb/schema.sql")
              .addScript("classpath:rewards/testdb/test-data.sql")
              .build();
      }   
  }
  </code></pre>
  
  <p>What exactly do? I think that all the beans configured in this class will belong to the <strong>jdbc-dev</strong> profile, but I am not sure about it. Can you give me more information about this thing?</p>
  
  <p>Why I have to use the <strong>@Profile</strong> annotation on a **configuration class* instead annotate directly my beans?</p>
  
  <p>Tnx</p>