当前位置: 首页 > 知识库问答 >
问题:

sping-data-cassandra:创建名为“会话工厂”的bean时出错,并且无法解析对bean“cassandraTemprint”的引用

束俊材
2023-03-14

我有一个 springboot 应用程序,我在其中连接到 cassandra DB。

我的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>

cassandraConfig 定义:

@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
public interface MCSEDistributorRepository extends CassandraRepository<MCSEDistributor, String> {

}

实体类:

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

        @Column
        private String name;
}

现在,当我启动应用程序时,我遇到了以下错误:

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

它说cassandraTemplate和sessionFactory的依赖和bean创建问题。我是使用spring-data的新手,所以不确定我错过了什么。

从启动日志,我可以看到它正在使用以下驱动程序版本:

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

共有3个答案

解高昂
2023-03-14

您的< code>@configuration类有问题。它没有< code>@bean注释。看看这个来自https://www.baeldung.com/spring-data-cassandra-tutorial的Cassandra < code > @ configuration 类的例子

@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();
    }
}

还有关于如何配置存储库和表的说明。

晏华奥
2023-03-14

尝试调用 super,然后设置所需的属性

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

    return cluster;
}
乔宏峻
2023-03-14

想通了问题。问题是CassandraConfig类缺少这个:

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

加上这个,它开始运行良好。

 类似资料:
  • 我使用hibernate 3.2.7和spring 3.2.8。错误是*** **“:创建ServletContext资源[/WEB-INF/Spring-Servlet.xml]中定义的名称为'session Factory'的bean时出错:init方法调用失败;嵌套异常为java.io.FileNotFoundException:ServletContext资源[/resources/hib

  • 我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage

  • 在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi

  • 我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus 到我现有的spring boot应用程序,但我得到以下错误: 用户类别: 控制器类: pom xml: 添加它

  • 我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪: