2015-05-01 64 views
0

你好我正在使用spring-boot和hibernate创建简单的webapp。 application.properties文件包含db连接的属性。 贝娄是我的文件:找不到SpringBoot存储库(休眠)

应用背景:

package hbwc; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.ComponentScan; 

@SpringBootApplication 
public class Application { 

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

Customer实体:

package hbwc.customer; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity 
public class Customer { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 
    private String firstName; 
    private String lastName; 

    protected Customer() { 
    } 

    public Customer(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    @Override 
    public String toString() { 
     return String.format(
       "Customer[id=%d, firstName='%s', lastName='%s']", 
       id, firstName, lastName); 
    } 

} 

客户资料库:

package hbwc.customer; 

import org.springframework.data.repository.CrudRepository; 

public interface CustomerRepository extends CrudRepository<Customer, Long> {} 

CustomerController:

package hbwc.customer; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
import java.util.List; 

@RestController 
public class CustomerController { 

    @Autowired 
    CustomerRepository repository; 

    @RequestMapping("/customers") 
     public String index() { 
     List<Customer> all = (List<Customer>) repository.findAll(); 
     return "ok"; 
    } 
} 

我使用gradlew建设和运行这个例子。但我有问题,而 运行它(./梯形图运行)。我收到了一个异常,告诉我找不到我的CustomerRepository。 堆栈跟踪:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hbwc.customer.CustomerRepository] 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:1301) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 
    ... 18 common frames omitted 

请给我一些建议什么是错我的代码。

application.properties(项目根目录)

spring.datasource.url=jdbc:mysql://localhost/hbwc 
spring.datasource.username=root 
spring.datasource.password=baza1 

spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=create-drop 
+0

您需要配置数据源,并且需要在类路径上使用弹簧数据的impl(例如spring-data-jpa)。请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html – mp911de

+0

在application.properties文件中拥有属性不够? – tomgal

+0

看看这里的例子看起来好吗https://spring.io/guides/gs/accessing-data-jpa/。您可以尝试向您的应用程序配置类添加'@Repository'到您的CustomerRepository接口或'@EnableJpaRepositories'。根据这个例子,这两者都不需要,但是无论如何都要尝试。 –

回答

0

你不必在你的仓库类中的任何分类元件注释。尝试添加@Repository或@Component。然后,您应该(假设启用了组件扫描)能够将存储库自动装入您的控制器。