title: Spring Scheduled Task
body: <p>I'm trying to create a Scheduled task with Spring, but I'm probably not doing something correctly exposing the @Bean in configuration. My code is as follows:</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:hibernate.properties")
  @EnableJpaRepositories("org.app.repository")
  @ComponentScan("org.app")
  @EnableTransactionManagement
  @EnableScheduling
  public class JpaConfiguration {
  
      private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
      private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
      private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN =
              "entitymanager.packages.to.scan";
  
      @Resource
      private Environment env;
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
          dataSource.setDriverClassName(
                  env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
          dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
          dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
          dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
  
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  
          // will set the provider to 'org.hibernate.ejb.HibernatePersistence'
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          // will set hibernate.show_sql to 'true'
          vendorAdapter.setShowSql(true);
          // if set to true, will set hibernate.hbm2ddl.auto to 'update'
          vendorAdapter.setGenerateDdl(true);
  
          LocalContainerEntityManagerFactoryBean emfBean = new
                  LocalContainerEntityManagerFactoryBean();
          emfBean.setDataSource(dataSource());
          emfBean.setJpaVendorAdapter(vendorAdapter);
          emfBean.setPersistenceProviderClass(
                  org.hibernate.jpa.HibernatePersistenceProvider.class);
  
          emfBean.setPackagesToScan(
                  env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
  
          emfBean.setJpaProperties(hibProperties());
  
          return emfBean;
      }
  
      private Properties hibProperties() {
          Properties properties = new Properties();
          properties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
                  env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
          properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
                  env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
          return properties;
      }
  
      @Bean
      public JpaTransactionManager transactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
          return transactionManager;
      }
  
      @Bean
      public ThreadPoolTaskScheduler taskScheduler() {
          ThreadPoolTaskScheduler ts = new ThreadPoolTaskScheduler();
          ts.initialize();
          ts.setPoolSize(8);
          ts.setWaitForTasksToCompleteOnShutdown(true);
          return ts;
      }
  }
  
  @Component
  public class MainBean {
  
      @Autowired
      private MyRunnable myRunnable;
  
      @Autowired
      private CategoryRepo categoryRepo;
  
      @Autowired
      private ThreadPoolTaskScheduler taskScheduler;
  
      public void start() {
  
              //This works
          categoryRepo.findAll().forEach(System.out::println);
  
  
          //this throws an exception
          taskScheduler.execute(myRunnable);
  
          //if i use an infinite loop here in order to prevent 
          //the method from exiting everything works normal
          // while(true) {
          //    Thread.sleep(10000000);
          // }
          System.out.println("Application Started. . .");
      }
  }
  </code></pre>
  
  <p><strong>Category Repo</strong></p>
  
  <pre><code>public interface CategoryRepo extends JpaRepository&lt;Category, String&gt; {
  
  }
  </code></pre>
  
  <p><strong>Runnable</strong></p>
  
  <pre><code>@Component
  public class MyRunnable implements Runnable {
  
  
  
      @Autowired
      private CategoryRepo categoryRepo;
  
      @Override
      public void run() {
  
  
          try {
              List&lt;Category&gt; list = categoryRepo.findAll();
              list.forEach(System.out::println);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Also if I dont use a scheduler and I just do </p>
  
  <pre><code>myRunnable.run(); 
  </code></pre>
  
  <p>it executes normally.</p>
  
  <p>Anyone have an idea what I'm doing wrong, or maybe an alternative way in doing this?</p>
  
  <p>EDIT: My pom.xml dependencies are as follows (Spring is 4.0.2 as you can see in the properties tag):</p>
  
  <pre><code>    &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;spring.version&gt;4.0.2.RELEASE&lt;/spring.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;!--Hibernate Dependencies--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;4.3.4.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
              &lt;version&gt;4.3.4.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;4.3.4.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;5.0.3.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;!-- Spring  Dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;1.5.1.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;!--CGLIB is required to process @Configuration classes--&gt;
          &lt;dependency&gt;
          &lt;groupId&gt;cglib&lt;/groupId&gt;
          &lt;artifactId&gt;cglib&lt;/artifactId&gt;
          &lt;version&gt;3.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!--Other Dependencies--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
              &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
              &lt;version&gt;1.7.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;1.7.6&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.17&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;5.1.28&lt;/version&gt;
          &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  </code></pre>
  
  <p>And Java is 1.8 - also tried with 1.7 and had same results</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Apparently I think the issue is that the program is exiting and it leaves the Spring Context. Therefore when the runnable is executed, the thread cant find the entitymanager.</p>
  
  <p>If I use an infinite while loop right before the MainBean exits then everything executes normally</p>