2017-08-28 89 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包中,以便覆盖所有子包,并且不需要考虑需要添加的所有附加注释。

相关问题