2015-02-11 192 views
2

我正在嘗試使用spring-data-cassandra將cassandra與Spring啓動集成。春季啓動cassandra集成@EnableCassandraRepositories沒有爲Cassandra Repository生成實現

Application.java

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[email protected]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: 'hiddenHttpMethodFilter' 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; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: dao.UserDao hello.UserController.userDao; nested 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; nested 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 

回答

5

似乎你的Dao類沒有註冊到上下文中。我會說它缺少一個像@Repository的適當的註釋。

此外,您的Application類生活在hello包中,沒有任何進一步的配置,只掃描下面的組件。這就是爲什麼它沒有找到CassandraConfiguration(住在conf)。然後這也不掃描dao包。

+0

最初我想,因爲我所提到的在@EnableCassandraRepositories基礎包(「道」)所以它會將該包下的所有類都視爲bean。但後來我也試圖在UserDao類上放置@Repository,但仍然不高興。 – Prashant 2015-02-11 12:07:01

+0

不,它只是標註哪個基本軟件包來掃描存儲庫。由於它不工作:你可以發佈堆棧跟蹤更新嗎? – 2015-02-11 12:31:17

+0

我剛更新了我的問題中的日誌部分。 – Prashant 2015-02-11 13:03:46

1

我也面臨自動佈線它使用cassandra支持類同樣的問題,嘗試添加:

@EnableCassandraRepositories(basePackages="package path where where is ur bean")