Spring-data-cassandra: Error creating bean with name 'sessionFactory' and Cannot resolve reference to bean 'cassandraTemplate'

喜你入骨 提交于 2020-07-10 08:34:11

问题


I have a springboot app, in which I am connecting to cassandra DB.

My pom.xml:

parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
    </dependency>

The cassandraConfig definitition:

@Configuration
@PropertySource("file:///Users/s1b03vf/Work/carrierhub/cass.properties")
@ConfigurationProperties()
@EnableCassandraRepositories(basePackageClasses = {MCSEDistributorRepository.class})
public class MSCECassandraConfig {
protected String contactPoints;

protected int port;

protected String username;

protected String password;

protected String keyspace;


@Override
protected AuthProvider getAuthProvider() {
    return new PlainTextAuthProvider(username, password);
}


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


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


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


@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example.demo.entity.cassandra"};
}
}

Repository class:

@Repository
public interface MCSEDistributorRepository extends CassandraRepository<MCSEDistributor, String> {

}

Entity class:

    @Table("mcse_offer")
    public class MCSEDistributor {
        @Column
        @PrimaryKey
        private int id;

        @Column
        private String name;
}

Now when I start my application I am running into the below error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/Users/s1b03vf/.m2/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/s1b03vf/.m2/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerMapping' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MCSEDistributorRepository': Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.core.CassandraAdminTemplate]: Factory method 'cassandraTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'session' defined in class path resource [com/example/demo/config/MSCECassandraConfig.class]: Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.DriverInternalError: Unexpected exception thrown

It says dependency and bean creation issue for cassandraTemplate and sessionFactory. I am new to using spring-data, so not sure what I am missing here.

From startup log, I can see that it is using the below driver version:

2020-06-08 22:44:57.782  INFO 42129 --- [  restartedMain] com.datastax.driver.core                 : DataStax Java driver 3.7.2 for Apache Cassandra

回答1:


There is something wrong with your @configuration class. it doesn't have @bean annotations. look at this example cassandra @configuration class from: https://www.baeldung.com/spring-data-cassandra-tutorial

@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {

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

    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = 
          new CassandraClusterFactoryBean();
        cluster.setContactPoints("127.0.0.1");
        cluster.setPort(9142);
        return cluster;
    }

    @Bean
    public CassandraMappingContext cassandraMapping() 
      throws ClassNotFoundException {
        return new BasicCassandraMappingContext();
    }
}

There is also explanation on how to configure the repository and table.




回答2:


Figured out the issue. Issue was that the CassandraConfig class was missing this:

@Override
    protected boolean getMetricsEnabled() {
        return false;
    } 

Added this and it started running fine.



来源:https://stackoverflow.com/questions/62267799/spring-data-cassandra-error-creating-bean-with-name-sessionfactory-and-cannot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!