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.
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.
<bean id="cassandratemplate" class="org.springframework.data.cassandra.core.CassandraTemplate"> <constructor-arg ref="cassandrasession" /> </bean>
@Autowired private CassandraTemplate cassandraTemplate;
# Cassandra Database Configuration
spring.data.cassandra.contact-points=localhost
spring.data.cassandra.port=9042
spring.data.cassandra.keyspace=my_keyspace
spring.data.cassandra.username=my_username
spring.data.cassandra.password=my_password
After encountering the error, we successfully resolved it by implementing the following configurations alongside the settings in the 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; } @Bean @Override public CqlSessionFactoryBean cassandraSession() { CqlSessionFactoryBean cassandraSession = super.cassandraSession(); 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() .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)) .build()); } }; } }
We integrated the provided configuration into our Spring application to resolve the issue.