Recently, I started working on a project where I'm using Spring Boot to connect to a Cassandra database. However, I'm encountering an error at startup error : "cannot resolve reference to bean 'cassandratemplate' while setting bean property 'cassandratemplate". In this post i'm going to discuess about the solution of that error.

Solution for Error: "Cannot resolve reference to bean 'cassandratemplate' while setting bean property 'cassandratemplate'"

Encountering the error "Cannot resolve reference to bean 'cassandratemplate' while setting bean property 'cassandratemplate'" indicates a configuration issue with our Spring application regarding the Cassandra template. 

Step to check that issue:

  1. First and foremost, let's ensure that Cassandra configuration is correctly set up in  Spring application context.
  2. You need to open your Spring application context configuration file (e.g., applicationContext.xml) and verify if the 'cassandratemplate' bean is defined accurately. It should resemble something like this:
  3.       <bean id="cassandratemplate" class="">
              <constructor-arg ref="cassandrasession" />
  4. We must ensure that the bean name specified in our XML configuration matches the bean name used in our Java code where we're trying to inject the 'cassandratemplate' bean.
  5. If you're using autowiring to inject dependencies, let's double-check that the 'cassandratemplate' bean is being autowired correctly. For example:
  6.       @Autowired
          private CassandraTemplate cassandraTemplate;
  7.  Confirm that all required dependencies for the 'cassandratemplate' bean are present in our project's classpath. We should ensure that the necessary Cassandra dependencies are configured in our Maven or Gradle build file.
  8. If you're utilizing component scanning to automatically detect Spring beans, let's ensure that the package containing the 'CassandraTemplate' class is included in the component scan base package.

# Cassandra Database Configuration

After encountering the error, we successfully resolved it by implementing the following configurations alongside the settings in the file:

@PropertySource(value = { "" })
@EnableCassandraRepositories(basePackages = "com.walmartlabs.cia.MatchReconciler.cassandra")
public class CassandraConfig extends AbstractCassandraConfiguration {

  private String keySpace;

  private String contactPoints;

  private String datacenter;
  private int port;
  private String username;

  private String password;
  private String keyspaceName;

  protected String getContactPoints() {
    return contactPoints;
  protected String getKeyspaceName() {
    return keySpace;

  protected int getPort() {
    return port;

  protected String getLocalDataCenter() {
    return datacenter;  

  public CqlSessionFactoryBean cassandraSession() {
    CqlSessionFactoryBean cassandraSession = super.cassandraSession();
    return cassandraSession;

  protected SessionBuilderConfigurer getSessionBuilderConfigurer() {
    return new SessionBuilderConfigurer() {

      public CqlSessionBuilder configure(CqlSessionBuilder cqlSessionBuilder) {"Configuring CqlSession Builder");
        return cqlSessionBuilder
                .withDuration(DefaultDriverOption.METADATA_SCHEMA_REQUEST_TIMEOUT, Duration.ofMillis(50000))
                .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofMillis(50000))
                .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(10000))

We integrated the provided configuration into our Spring application to resolve the issue. 

  • Additional Configuration: By adding a custom Cassandra configuration class, we could fine-tune our Cassandra settings beyond what's specified in the file.
  • Injection of Properties: We utilized the @Value annotation to inject property values from the file into our Java configuration class.
  • Customization of Cassandra Session: We extended AbstractCassandraConfiguration to customize the Cassandra session properties according to our requirements.
  • Timeout Configuration: Notably, we adjusted the timeout configurations to prevent timeout errors during database operations.