Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'

551 views Asked by At

I am using spring boot to connect to Cassandra Database.I am getting an error at startup that says

Error creating bean with name 'abcItemsRepository' defined in com.myspace.mycompany.repository defined in @EnableCassandraRepositories declared on CassandraRepositoriesRegistrar.EnableCassandraRepositoriesConfiguration: Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'

Please help me in fixing it. Following are my configurations and repositories:

@Table("abc_items")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OboItems {

  @Id
  @PrimaryKeyColumn(name = "item_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
  @Column(name = "item_id")
  private String itemId;

  @Column(name = "last_scheduled_time")
  private Date lastScheduledTime;

}

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}


public interface OboItemsRepository extends CassandraRepository<OboItems,String> {

}


spring.data.cassandra.contact-points=<my-end-point>
spring.data.cassandra.port=9042
spring.data.cassandra.username=<username>
spring.data.cassandra.password=<password>
spring.data.cassandra.keyspace-name=<mykeyspace>
spring.data.cassandra.session.timeout=30000

I tried Configuring CassandraConfig that extends from AbstractCassandraConfiguration.But I had no luck with it. I am expecting the application to startup successfully and be able to run the cassandra queries in JPA style.

1

There are 1 answers

1
Omprakash On

I was able to fix it using following cofigurations in addition to configurations in application.properties file.

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

  @Value("${spring.data.cassandra.keyspace-name}")
  private String keySpace;

  @Value("${spring.data.cassandra.contact-points}")
  private String contactPoints;

  @Value("${spring.data.cassandra.dc}")
  private String datacenter;
  @Value("${spring.data.cassandra.port}")
  private int port;
  @Value("${spring.data.cassandra.username}")
  private String username;

  @Value("${spring.data.cassandra.password}")
  private String password;
  @Value("${spring.data.cassandra.keyspace-name}")
  private String keyspaceName;

  @Override
  protected String getContactPoints() {
    return contactPoints;
  }
  @Override
  protected String getKeyspaceName() {
    return keySpace;
  }


  @Override
  protected int getPort() {
    return port;
  }

  @Override
  protected String getLocalDataCenter() {
    return datacenter;  // Adjust as per your setup
  }

  @Bean
  @Override
  public CqlSessionFactoryBean cassandraSession() {
    CqlSessionFactoryBean cassandraSession = super.cassandraSession();//super session should be called only once
    cassandraSession.setUsername(username);
    cassandraSession.setPassword(password);
    cassandraSession.setContactPoints(contactPoints);
    cassandraSession.setPort(port);
    cassandraSession.setLocalDatacenter(datacenter);
    cassandraSession.setKeyspaceName(keySpace);
    return cassandraSession;
  }

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

      @Override
      public CqlSessionBuilder configure(CqlSessionBuilder cqlSessionBuilder) {
        log.info("Configuring CqlSession Builder");
        return cqlSessionBuilder
                .withConfigLoader(DriverConfigLoader.programmaticBuilder()
                // Resolves the timeout query 'SELECT * FROM system_schema.tables' timed out after PT2S
                .withDuration(DefaultDriverOption.METADATA_SCHEMA_REQUEST_TIMEOUT, Duration.ofMillis(60000))
                .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofMillis(60000))
                .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(15000))
                .build());
      }
    };
  }