spring-projects / spring-data-cassandra

Provides support to increase developer productivity in Java when using Apache Cassandra. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-cassandra/
Apache License 2.0
379 stars 311 forks source link

Spring Boot 3: AbstractCassandraConfiguration not creating cassandraTemplate with different bean names #1411

Closed sathishnune closed 1 year ago

sathishnune commented 1 year ago

After Spring Boot 3 upgrade, AbstractCassandraConfiguration failed to create a cassandraTemplate with different bean name.

@Configuration
@EnableCassandraRepositories
public class TestCassandraConfig extends AbstractCassandraConfiguration {

    @Override
    protected String getKeyspaceName() {
        return "keyspace";
    }

    @Override
    protected String getContactPoints() {
        return "localhost:9042";
    }

    @Override
    protected String getLocalDataCenter() {
        return "datacenter1";
    }

    @Override
    @Bean("cassandraHealthCheckSession")
    public CqlSessionFactoryBean cassandraSession() {
        CqlSessionFactoryBean cassandraSession = super.cassandraSession();
        cassandraSession.setUsername("username");
        cassandraSession.setPassword("password");

        return cassandraSession;
    }

    @Bean("cassandraHealthCheckTemplate")
    @Override
    public CassandraAdminTemplate cassandraTemplate() {
        return new CassandraAdminTemplate(getRequiredSession(), requireBeanOfType(CassandraConverter.class));
    }

    @Bean
    @Override
    public SessionFactoryFactoryBean cassandraSessionFactory(CqlSession cassandraHealthCheckSession) {
        return super.cassandraSessionFactory(cassandraHealthCheckSession);
    }

    @Override
    protected CqlSession getRequiredSession() {
        return getBeanFactory().getBean("cassandraHealthCheckSession", CqlSession.class);
    }

    @Override
    protected SessionFactory getRequiredSessionFactory() {
        return this::getRequiredSession;
    }
}

Error: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [com/example/cassandratest/config/TestCassandraConfig.class]: No matching factory method found on class [com.example.cassandratest.config.TestCassandraConfig]: factory bean 'testCassandraConfig'; factory method 'cassandraTemplate()'. Check that a method with the specified name exists and that it is non-static.

I need multiple templates as I need to connect to multiple key spaces. Is there a way to make it work?

mp911de commented 1 year ago

If you want to connect to different keyspaces, you have generally multiple options of which having many template instances is the last one to consider.

  1. Provide a routing SessionFactory based on AbstractRoutingSessionFactory
  2. Apply keyspace routing on per-Statement level. SimpleStatement allows keyspace routing to a different keyspace. By customizing CqlTemplate you can set the keyspace if needed and If your Cassandra version supports keyspace routing
  3. Having multiple templates requires you to manually spin up all required infrastructure components. Since Spring Data cannot know which components you want to share (MappingContext, Converter, Templates, Repositories), you will need to define all beans and their relationships to each other in your configuration.
sathishnune commented 1 year ago

I am trying to use 3rd option to create multiple cassandraTemplates, then pass respective template to cassandraTemplateRef.

Same piece(above mentioned config) of logic is working fine with 2.7.x but failing with new Spring Boot 3.x. Would like to know what is changed & how to fix.

mp911de commented 1 year ago

If you want to know the details then please provide additional context in the form of the full stack trace.

spring-projects-issues commented 1 year ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues commented 1 year ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.