title: BeanCreationException in accessing @Bean in @Configuration class
body: <p>I am trying to access the @Bean class in my another class but I keep on getting BeanCreationException.</p>
  
  <p>Here is my config file with all @Bean classes</p>
  
  <p>CassandraConfig.class</p>
  
  <pre><code>package com.spring.cassandra.daos;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
  import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
  import org.springframework.data.cassandra.config.SchemaAction;
  import org.springframework.data.cassandra.convert.CassandraConverter;
  import org.springframework.data.cassandra.convert.MappingCassandraConverter;
  import org.springframework.data.cassandra.core.CassandraOperations;
  import org.springframework.data.cassandra.core.CassandraTemplate;
  import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
  import org.springframework.data.cassandra.mapping.CassandraMappingContext;
  
  @Configuration
  @PropertySource(value = { "classpath:com/spring/cassandra/props/cassandra.properties" })
  public class CassandraConfig {
      public CassandraConfig(){
          System.out.println("In Cassandra Config");
      }
  
      @Autowired
      private Environment env;
  
      @Bean
      public CassandraClusterFactoryBean cluster() {
  
          CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
          cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
          cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));
  
          return cluster;
      }
  
      @Bean
      public CassandraMappingContext mappingContext() {
          return new BasicCassandraMappingContext();
      }
  
      @Bean
      public CassandraConverter converter() {
          return new MappingCassandraConverter(mappingContext());
      }
  
      @Bean
      public CassandraSessionFactoryBean session() throws Exception {
  
          CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
          session.setCluster(cluster().getObject());
          session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
          session.setConverter(converter());
          session.setSchemaAction(SchemaAction.NONE);
  
          return session;
      }
  
      @Bean
      public CassandraOperations cassandraTemplate() throws Exception {
          return new CassandraTemplate(session().getObject());
      }
  }
  </code></pre>
  
  <p>CassandraTransaction.class</p>
  
  <pre><code>package com.spring.cassandra.daos;
  
  import java.util.List;
  
  import org.springframework.cassandra.core.RowMapper;
  import org.springframework.config.java.context.JavaConfigApplicationContext;
  import org.springframework.data.cassandra.core.CassandraOperations;
  import org.springframework.data.cassandra.core.CassandraTemplate;
  import org.springframework.stereotype.Component;
  
  import com.datastax.driver.core.Row;
  import com.datastax.driver.core.exceptions.DriverException;
  
  @Component
  public class CassandraTransaction {
  
  /*Error in this below two lines while trying to access the CassandraTemplate.class*/
  
  
      JavaConfigApplicationContext context = new JavaConfigApplicationContext(CassandraConfig.class);
      CassandraOperations cassandraOperations = (CassandraOperations) context.getBean(CassandraTemplate.class);
  
      private CassandraTransaction(){
          System.out.println("In Cassandra Transaction");
          getUsers();
      }
  
      public void getUsers(){
          System.out.println("Retreiving users");
          List&lt;User&gt; results = cassandraOperations.query("select * from users", new RowMapper&lt;User&gt;() {
              @Override
              public User mapRow(Row row, int rowNum) throws DriverException {
                  User user = new User();
                  user.setUsername(row.getString("username"));
                  user.setName(row.getString("name"));
                  user.setEmail(row.getString("email"));
                  return user;
              }
          });
  
          for(User user : results){
              System.out.println(user);
          }
      }
  }
  </code></pre>
  
  <p>Exception in console:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTransaction' defined in file [D:\Spring\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\CassandraApplication\WEB-INF\classes\com\spring\cassandra\daos\CassandraTransaction.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.spring.cassandra.daos.CassandraTransaction]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
  </code></pre>
  
  <p>I am in need of access the CassandraOperations cassandraTemplate() bean in order to use the template to query the cassandra DB but I am facing this error in accessing the bean class.</p>
  
  <p>Should we use ApplicationContext only in main() methods? I see many examples using ApplicationContext in <code>public static void main(){}</code>, if so how could that be possible using main method in spring? I am confused on what has to be done. Please explain.</p>