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
您需要配置數據源,並且需要在類路徑上使用彈簧數據的impl(例如spring-data-jpa)。請參閱http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html – mp911de
在application.properties文件中擁有屬性不夠? – tomgal
看看這裏的例子看起來好嗎https://spring.io/guides/gs/accessing-data-jpa/。您可以嘗試向您的應用程序配置類添加'@Repository'到您的CustomerRepository接口或'@EnableJpaRepositories'。根據這個例子,這兩者都不需要,但是無論如何都要嘗試。 –