2017-08-28 204 views
0

我正在使用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不解決我的問題。

+1

Spring引導程序只從它定義的包開始掃描包。你的應用程序類在'com.example.demo'包中,其他所有東西都不是基本上沒有被檢測到。最好的做法是將你的應用程序類放在最適合你的最高層包中,在你的情況下將它移動到'com.example'。否則你需要添加'@ ComponentScan','@ EntityScan'和'@ EnableJpaRepositories'指向正確的軟件包(可行但更多的工作)。 –

+0

@ M.Deinum應該是一個答案(和接受和upvoted之一) –

回答

0

當使用@SpringBootApplication自動暗示@ComponentScan@ComponentScan的默認行爲是,當沒有明確定義basePackages時,應從包中定義的類開始掃描。春季啓動,這也適用於所有其他自動配置如檢測機構,檢測彈簧數據倉庫等

現在,作爲你的CatWebServiceApplicationcom.example.democom.example.dao後您的ICandidate定義將不會被掃描,因爲它不是」 t是com.example.demo包的一部分。

有幾種方法可以解決這個問題。

首先,你可以在@SpringBootApplication指定scanBasePackages有檢測部件,但是這不會解決這個問題,你還需要@EnableJpaRepositories("com.example.dao")@EntityScan("com.example.dao"),也許一對夫婦更擴展技術時。

最簡單和推薦的方法是將CatWebServiceApplication放在com.example包中,以便覆蓋所有子包,並且不需要考慮需要添加的所有附加註釋。

相關問題