我正在使用Eclipse和Maven與Java 1.8試圖構建一個基於maven項目的spring項目,所以我構建了自己的項目實體underName Candidat
這個完整的代碼塊需要一個無法找到的類型爲'com.example.dao.InterfaceName'的bean
package com.example.demo.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "CandidatTable")
public class Candidat implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
private String prenom;
private String reference;
private String resumeCandidate;
public Candidat() {
super();
}
public Candidat(String name, String prenom, String reference, String resumeCandidate) {
super();
this.name = name;
this.prenom = prenom;
this.reference = reference;
this.resumeCandidate = resumeCandidate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getResumeCandidate() {
return resumeCandidate;
}
public void setResumeCandidate(String resumeCandidate) {
this.resumeCandidate = resumeCandidate;
}
}
在正常情況下,我們應該建立一個接口到它,我們應該這樣定義,我們說我們的服務方法:save()
,findAllRecords()
,findSpecificRecordByID()
,updateRecordLine()
,deleteRecord()
...等,但在我的情況下,我在我的maven項目中使用了Spring-data,在我的web.xml文件中,我有這種依賴關係:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
所以沒有必要定義我們的方法,因爲Spring的數據的情況下使用其自己的方法,我們創造它擴展了通用接口JpaRepository
的接口,所以我的界面是這樣的:
package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entities.Candidat;
public interface ICandidate extends JpaRepository<Candidat, Long>{
//no need to define our methods, because we gonna use methods difined
// by SpringData whose comes from JPA specification.
}
最後,主類代碼,這是一個:
package com.example.demo;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.dao.ICandidate;
import com.example.demo.entities.Candidat;
@SpringBootApplication
public class CatWebServiceApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(CatWebServiceApplication.class, args);
ICandidate condidateRep = context.getBean(ICandidate.class);
condidateRep.save(
new Candidat("UserName_1", "FirstName_1", "Ref_1", "/Home/file/docFile_1.docx")); //insert data using Ioc later after runing urself
condidateRep.save(
new Candidat("UserName_2", "FirstName_2", "Ref_2", "/Home/file/docFile_2.docx"));
List<Candidat> cnds = condidateRep.findAll();
cnds.forEach(p-> System.out.println(p.getResumeCandidate()));
}
}
我的應用程序應該把好了,看向web.xml中管理應用dependcies,那麼它的廁所ķ到路徑src/main/resources
包含文件application.properties
包含此代碼:
# DataSource settings:
spring.datasource.url = jdbc:mysql://localhost:3306/softherWebService
spring.datasource.username = root
spring.datasource.password = dbPassword
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
突然,我得到這個錯誤信息我的日食控制檯上:
異常線程「main」 org.springframework。 beans.factory.NoSuchBeanDefinitionException:無型 '
com.example.dao.ICandidate
' 的 預選賽豆可在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) 在 com.example.demo .CatWebServiceApplication.main(CatWebServiceApplication.java:19)
所以的getBean問題()行,我用了CommandLineRunner太多,但在得到Bean不解決我的問題。
Spring引導程序只從它定義的包開始掃描包。你的應用程序類在'com.example.demo'包中,其他所有東西都不是基本上沒有被檢測到。最好的做法是將你的應用程序類放在最適合你的最高層包中,在你的情況下將它移動到'com.example'。否則你需要添加'@ ComponentScan','@ EntityScan'和'@ EnableJpaRepositories'指向正確的軟件包(可行但更多的工作)。 –
@ M.Deinum應該是一個答案(和接受和upvoted之一) –