程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现?

开发过程中遇到Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现的解决方法建议,希望对你解决Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现有所启发或帮助;

似乎您的Dao类没有被注册到上下文中。我会说它缺少像@Repository这样的适当注释。

另外,您的Application班级生活在Hello软件包中,并且没有任何进一步的配置,仅扫描它下面的组件。这就是为什么它没有找到CassandraConfiguration(居住在conf)。然后,这也不会扫描dao程序包。

解决方法

我正在尝试使用spring-data-Cassandra将Cassandra与Spring Boot集成。

应用程序

package Hello;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.Annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

CassandraConfiguration.java

package conf;

import org.springframework.context.Annotation.bean;
import org.springframework.context.Annotation.Configuration;
import org.springframework.data.Cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.Cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.Cassandra.mapping.basicCassandraMappingContext;
import org.springframework.data.Cassandra.mapping.CassandraMappingContext;
import org.springframework.data.Cassandra.repository.config.EnableCassandraRepositories;


@Configuration
@EnableCassandraRepositories("dao")
public class CassandraConfiguration extends AbstractCassandraConfiguration {

    @Bean
    @Override
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints("localhost");
        cluster.setPort(9042);
        return cluster;
    }

    @Override
    protected String getKeyspacename() {
        return "mykeyspace";
    }

    @Bean
    @Override
    public CassandraMappingContext CassandraMapping() throws  ClassnotFoundException {
         return new BasicCassandraMappingContext();
    }

}

UserDao.java

package dao;

import Hello.User;
import org.springframework.data.Cassandra.repository.CassandraRepository;
import org.springframework.data.Cassandra.repository.Query;

public interface UserDao extends CassandraRepository<User> {

    @Query("SELEct * from users where fname = ?0")
    Iterable findByFname(String fName);
}

RestController.java

package Hello;

import dao.UserDao; 
import org.springframework.beans.factory.Annotation.Autowired;
import org.springframework.web.bind.Annotation.requestMapping;
import org.springframework.web.bind.Annotation.requestParam;
import org.springframework.web.bind.Annotation.RestController;


    @RestController
    public class UserController {

    @Autowired
    UserDao userDao;


    @requestMapping("/greeTing")
    public Iterable<User> geTinfo(@requestParam(value="name",DefaultValue="Hello") String name,@requestParam(value="lname",DefaultValue="World") String lName) {
        return userDao.findByFname(Name) ;//new User(counter.incrementAndGet(),name,lName);
    }
}

User.java

package Hello;

import org.springframework.data.Cassandra.mapping.column;
import org.springframework.data.Cassandra.mapping.primaryKey;
import org.springframework.data.Cassandra.mapping.Table;

@Table
public class User {

    @primaryKey
    private final long id;
    @column
    private final String fname;
    @column
    private final String lname;

    public User(long id,String fname,String lName) {
        this.id = id;
        this.fname = fname;
        this.lname = lname;
    }

    public long getId() {
        return id;
    }

    public String getFname() {
        return fname;
    }

    public String getLname() {
        return lname;
    }

}

在后台@EnableCassandraConfiguration应该为UserDao接口创建一个实现。但似乎出于某种原因未这样做。日志对于告诉我在这里犯下的具体错误不是很有用。仍在寻求帮助,我将其张贴在这里。

2015-02-11 12:10:58.424  INFO 7828 --- [           main] Hello.Application                        : StarTing Application on HOTCPC9941 with PID 7828 (C:\Users\prashant.tiwari\Documents\NetBeansProjects\DemoApp\target\classes started by prashant.tiwari in C:\Users\prashant.tiwari\Documents\NetBeansProjects\DemoApp)
2015-02-11 12:10:58.459  INFO 7828 --- [           main] ationConfigEmbeddedWebApplicationContext : refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f5737ca: startup date [Wed Feb 11 12:10:58 GMT 2015]; root of context hierarchy
2015-02-11 12:10:59.141  INFO 7828 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beAnnameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencycheck=0; autowireCandidate=true; priMary=false; factoryBeAnname=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factorymethodName=beAnnameViewResolver; initMethodName=null; destroymethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencycheck=0; autowireCandidate=true; priMary=false; factoryBeAnname=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factorymethodName=beAnnameViewResolver; initMethodName=null; destroymethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-02-11 12:10:59.865  INFO 7828 --- [           main] .t.tomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2015-02-11 12:11:00.035  INFO 7828 --- [           main] o.apache.catalina.core.Standardservice   : StarTing service tomcat
2015-02-11 12:11:00.036  INFO 7828 --- [           main] org.apache.catalina.core.StandardENGIne  : StarTing Servlet ENGIne: Apache tomcat/7.0.57
2015-02-11 12:11:00.127  INFO 7828 --- [ost-startStop-1] o.a.c.c.C.[tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-02-11 12:11:00.128  INFO 7828 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1672 ms
2015-02-11 12:11:00.555  INFO 7828 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-02-11 12:11:00.557  INFO 7828 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenhttpR_773_11845@ethodFilter' to: [/*]
2015-02-11 12:11:00.682  WARN 7828 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt

org.springframework.beans.factory.beanCreationException: Error creaTing bean with name 'userController': Injection of autowired dependencies failed; @R_489_11335@ted exception is org.springframework.beans.factory.beanCreationException: Could not autowire field: dao.UserDao Hello.UserController.userDao; @R_489_11335@ted exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency Annotations: {@org.springframework.beans.factory.Annotation.Autowired(required=truE)}
    at org.springframework.beans.factory.Annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at Hello.Application.main(Application.java:12)

Caused by: org.springframework.beans.factory.beanCreationException: Could not autowire field: dao.UserDao Hello.UserController.userDao; @R_489_11335@ted exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency Annotations: {@org.springframework.beans.factory.Annotation.Autowired(required=truE)}
    at org.springframework.beans.factory.Annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
    at org.springframework.beans.factory.Annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.Annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
    ... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency Annotations: {@org.springframework.beans.factory.Annotation.Autowired(required=truE)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    at org.springframework.beans.factory.Annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
    ... 18 common frames omitted

大佬总结

以上是大佬教程为你收集整理的Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现全部内容,希望文章能够帮你解决Spring Boot Cassandra集成@EnableCassandraRepositories未为Cassandra存储库生成实现所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。