当前位置:  开发笔记 > 运维 > 正文

如何自动装配spring-data CrudRepository

如何解决《如何自动装配spring-dataCrudRepository》经验,为你挑选了1个好方法。

一些细节我在运行下面给出的代码时遇到了一些问题.我收到以下异常.当我尝试[CrudRepository for Spring Data]的示例代码时[1].

我有一个界面:

package com.joydeep.springboot; 

import org.springframework.data.repository.CrudRepository; 
import com.joydeep.springboot.vo.Message; 

public interface Test1 extends CrudRepository { }

VO类:

package com.joydeep.springboot.vo;

import java.util.Date;    
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Message {
    @Id
    private String id;
    private String text;
    private String author;
    private Date created;

    public Message() {
        super();
    }
    public Message(String id, String text, String author, Date created) {
        super();
        this.id = id;
        this.text = text;
        this.author = author;
        this.created = created;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    }

控制器类:

package com.joydeep.springboot; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController;  

@RestController 
public class HelloCntrl { 

   @Autowired 
   Test1 test; 

   @RequestMapping("/hello")
   public String sayHi(){
      return "Hi"; 
   } 

}

初始化程序类:

package com.joydeep.springboot; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class CourseAPIStarter { 
   public static void main(String[] args) {
      SpringApplication.run(CourseAPIStarter.class); 
   } 
}

pom.xml中:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    io.springboot
    rest-course-api
    0.0.1-SNAPSHOT
    Rest service course API
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
    
    
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.apache.derby
            derby
            runtime
        

    

在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'helloCntrl'的bean时出错:通过字段'test'表示的不满意的依赖性; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.joydeep.springboot.Test1'类型的限定bean可用:预计至少有1个bean有资格作为autowire候选者.依赖注释{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

我所指的例子来自这个链接.

https://spring.io/guides/gs/accessing-data-jpa/

小智.. 12

您不需要添加@Repository注释Test1.将这3个注释放在你的CourseAPIStarter课堂上,你的应用程序将像黄油中的热刀一样运行.

@ComponentScan(basePackages={"com.joydeep.springboot"}) 

@EntityScan(basePackages={"com.joydeep.springboot.vo"}) 

@EnableJpaRepositories(basePackages={"com.joydeep.springboot"}) 

@ComponentScan 将扫描您的对象,并将其注册到Spring.

@EnableJpaRepositories 将允许您的所有Spring JPA存储库在您的应用程序中使用.

@EntityScan:当您@Entity在模型对象类上添加注释时,Spring将假定一个表存在名称Message.因此,要在Spring中注册此类,因为Entity您需要使用此注释.



1> 小智..:

您不需要添加@Repository注释Test1.将这3个注释放在你的CourseAPIStarter课堂上,你的应用程序将像黄油中的热刀一样运行.

@ComponentScan(basePackages={"com.joydeep.springboot"}) 

@EntityScan(basePackages={"com.joydeep.springboot.vo"}) 

@EnableJpaRepositories(basePackages={"com.joydeep.springboot"}) 

@ComponentScan 将扫描您的对象,并将其注册到Spring.

@EnableJpaRepositories 将允许您的所有Spring JPA存储库在您的应用程序中使用.

@EntityScan:当您@Entity在模型对象类上添加注释时,Spring将假定一个表存在名称Message.因此,要在Spring中注册此类,因为Entity您需要使用此注释.

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有