title: Spring Boot ignores beans from Java config class using Spring Profile
body: <p>When I run my application on Eclipse with a classical context loading, no worries, the beans defines on the config class corresponding with the Spring Profile chosen are correctly instanciated.</p>
  
  <pre><code>public class BasketHandlerLoader {
  
      public static void main(String[] args) throws Exception {
          @SuppressWarnings("resource")
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/spring/spring-archibald-basket-handler-context.xml");
          context.registerShutdownHook();
      }
  }
  </code></pre>
  
  <p>But, when I run the application with Spring Boot, these beans are not instanciated.</p>
  
  <pre><code>@Configuration
  @ImportResource("classpath:config/spring/spring-archibald-basket-handler-context.xml")
  public class BasketHandlerLoader {
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(BasketHandlerLoader.class, args);
      }
  }
  </code></pre>
  
  <p>Here is the java configuration class for the "dev" Spring profile:</p>
  
  <pre><code>@Configuration
  @Profile("dev")
  @EnableTransactionManagement
  @PropertySources(value = { @PropertySource("classpath:filters/dev.properties") })
  public class DevPersistenceConfig extends AbstractPersistenceConfig {
  
      @Inject
      private Environment env;
  
      @Override
      @Bean
      public DataSource dataSource() {
          return super.createDataSource(env);
      }
  
      @Override
      public Properties hibernateProperties() {
          return super.createHibernateProperties(env);
      }
  }
  </code></pre>
  
  <p>Here the <em>AbstractPersistenceConfig</em> class containing the others beans not instanciated:</p>
  
  <pre><code>public abstract class AbstractPersistenceConfig {
  
      // Constants...
  
      // ************************** ABSTRACT METHODS **************************
  
      /**
      * Returns a property list containing the Hibernate properties.
      * 
      * @return the Hibernate properties.
      */
      public abstract Properties hibernateProperties();
  
      /**
      * Defines the application datasource bean corresponding with the current Spring Profile.
      * 
      * @return the application datasource bean corresponding with the current Spring Profile.
      */
      @Bean
      public abstract DataSource dataSource();
  
      /**
      * Defines the Hibernate session factory bean.
      * 
      * @return the {@code LocalSessionFactoryBean}.
      */
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan(new String[] { HIBERNATE_PACKAGE_TO_SCAN });
          sessionFactory.setHibernateProperties(hibernateProperties());
          return sessionFactory;
      }
  
      /**
      * Defines the bean allowing to Hibernate to support the transaction handling mechanism.
      * 
      * @return the {@code HibernateTransactionManager}.
      */
      @Bean
      public HibernateTransactionManager transactionManager() {
          HibernateTransactionManager txManager = new HibernateTransactionManager();
          txManager.setSessionFactory(sessionFactory().getObject());
          return txManager;
      }
  
      // ************************** PROTECTED METHODS **************************
  
      // ...
  
      // ************************** PRIVATE METHODS **************************
  
      // ...
  }
  </code></pre>
  
  <p>I tried to run the application with the following commands with the same result:</p>
  
  <pre><code>java -jar archibald-basket-handler-1.0-SNAPSHOT.jar --spring.profiles.active=dev
  
  java -jar -Dspring.profiles.active=dev archibald-basket-handler-1.0-SNAPSHOT.jar
  </code></pre>
  
  <p>Concretely, the bean "<em>sessionFactory</em>" is not instanciated and cannot be injected into my <em>GenericDaoImpl</em> class...</p>
  
  <p>The stacktrace:</p>
  
  <pre><code>java.lang.reflect.InvocationTargetException
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
          at java.lang.Thread.run(Unknown Source)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basketDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory fr.ina.archibald.dao.impl.GenericDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
          at fr.ina.archibald.basket.loader.BasketHandlerLoader.main(BasketHandlerLoader.java:30)
          ... 6 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory fr.ina.archibald.dao.impl.GenericDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
          ... 21 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.     Dependency annotations: {@javax.inject.Inject()}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
          ... 23 more
  </code></pre>
  
  <p>I use the Spring Boot 1.0.2.RELEASE without the Spring Boot parent POM. I just defines this on the POM :</p>
  
  <pre><code>&lt;dependencyManagement&gt;
      &lt;dependency&gt;
          &lt;!-- Import dependency management from Spring Boot --&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
          &lt;version&gt;${org.springframework.boot.version}&lt;/version&gt;
          &lt;type&gt;pom&lt;/type&gt;
          &lt;scope&gt;import&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencyManagement&gt;
  
  &lt;plugin&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;goals&gt;
                  &lt;goal&gt;repackage&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
      &lt;configuration&gt;
          &lt;mainClass&gt;${start-class}&lt;/mainClass&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-integration&lt;/artifactId&gt;
      &lt;version&gt;${org.springframework.boot.version}&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Do you have some ideas?</p>
  
  <p>Thanks a lot!!</p>