title: Spring Profiles org.springframework.beans.factory.NoSuchBeanDefinitionException
body: <p>My sample project is Maven based structure, all my application proeprties files under <code>src/main/resources</code> folder. Below is the complete sample code. I am not understanding why my code not able to find profiles properly unless I use <code>@PropertySource</code> annotation. </p>
  
  <p><strong>My actual doubt is:</strong>  I've configured spring properties pretty well in application.properties file, but yet why it cannot find profile and their respective property files? Unless I am using <code>@PropertySource</code> annotation, iam not getting value for env.getProperty("mysql.url"). I mean <code>Environment</code> class not able to pick up values from profiles property files. <strong>WHY?</strong></p>
  
  <p><strong>Iam getting error as below:</strong></p>
  
  <pre><code>Jul 08, 2017 7:54:26 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
  INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@300ffa5d: startup date [Sat Jul 08 19:54:26 IST 2017]; root of context hierarchy
  helloBean
  Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'datasource' available
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
      at com.oreilly.datasource.Main2.main(Main2.java:15)
  </code></pre>
  
  <p><strong>DatasourceConfig.java</strong></p>
  
  <pre><code>package com.oreilly.datasource;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.dbcp.BasicDataSource;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Profile;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.context.annotation.PropertySources;
  import org.springframework.core.env.Environment;
  
  @Configuration
  /*@PropertySource("classpath:/application.properties")
  @PropertySource("classpath:/dev/application-dev.properties")
  @PropertySource("classpath:/prod/application-prod.properties")*/
  public class DatasourceConfig {
  
      @Autowired
      private Environment env;
  
      @Bean(name="helloBean")
      public String helloWorld() {
          System.out.println("helloBean");
          return "helloWorld....";
      }
  
      @Bean(name="datasource")
      @Profile("dev")
      public DataSource datasourceForDev(){
          BasicDataSource dataSource = new BasicDataSource();
          System.out.println(env.getProperty("mysql.url"));
          return dataSource;
      }
  
      @Bean(name="datasource")
      @Profile("prod")
      public DataSource datasourceForProd(){
          BasicDataSource dataSource = new BasicDataSource();
          System.out.println(env.getProperty("mysql.url"));
          return dataSource;
      }
  }
  </code></pre>
  
  <p><strong>Main2.java</strong></p>
  
  <pre><code>package com.oreilly.datasource;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.dbcp.BasicDataSource;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  
  public class Main2 {
  
  
      public static void main(String[] args) {
          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DatasourceConfig.class);
  
          DataSource dataSource = context.getBean("datasource", DataSource.class);
          String helloBean = context.getBean("helloBean", String.class);
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>application.properties</strong></p>
  
  <pre><code>spring.profiles.active=prod
  spring.config.name=application
  spring.config.location=classpath:/application.properties,classpath:/dev/application-dev.properties,classpath:/prod/application-dev.properties
  </code></pre>
  
  <p><strong>Below is the project folder structure:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ygl9t.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ygl9t.png" alt="enter image description here"></a></p>
  
  <p><strong>Please tell me what went wrong?</strong></p>