Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring引导中自动连接接口错误_Java_Spring_Spring Boot_Spring Mvc - Fatal编程技术网

Java 在Spring引导中自动连接接口错误

Java 在Spring引导中自动连接接口错误,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,在SpringBoot中将存储库接口连接到服务类时,我目前面临这个问题。 这是我的项目类 包com.ensa.entity import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Item { @Id @Gener

在SpringBoot中将存储库接口连接到服务类时,我目前面临这个问题。 这是我的项目类 包com.ensa.entity

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Item {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private float prix;

    public Item(Long id,String name,float prix) {
        this.id=id;
        this.name=name;
        this.prix=prix;
    }
    public Item() {

    }
    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 float getPrix() {
        return prix;
    }
    public void setPrix(float prix) {
        this.prix = prix;
    }}
这是我的存储库界面

package com.ensa.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ensa.entity.Item;



@Repository
@Component
public interface ItemRepository extends JpaRepository<Item, Long> {

}
关于如何解决这个问题有什么想法吗?

您不需要在Repository类中添加@Repository或@Component。从存储库中删除这些注释。就像

public interface ItemRepository extends JpaRepository<Item, Long> {

}
您不需要在Repository类中添加@Repository或@Component。从存储库中删除这些注释。就像

public interface ItemRepository extends JpaRepository<Item, Long> {

}
在main类中使用@EnableJPArepositorycom.ensa.repository,如果不尝试使用下面的代码行,它可能会工作

@ComponentScancom.ensa.repository在您的服务类中它可能工作

在主类中使用@EnableJPArepositorycom.ensa.repository,它可能工作,如果不尝试使用以下行

@ComponentScancom.ensa.repository在您的服务类中它可能会起作用,@repository已经是一个原型注释,因此您不需要@Component。 您可以从存储库中删除@Component注释

另外,在整个类上使用@Transactional也不是一个好的做法,因为它会降低代码的速度,因为数据库需要在使用@Transactional注释的整个代码执行时锁定。

存储库已经是一个原型注释,因此不需要使用@Component。 您可以从存储库中删除@Component注释


另外,在整个类上使用@Transactional也不是一个好的做法,因为它会降低代码的速度,因为数据库需要在用@Transactional注释的整个代码执行时锁定。

而其他的都是正确的,您可以在@Repository上省略@Component,您的实际错误是找不到@Bean

@SpringBootApplication主类上的注释包括@ComponentScan,这意味着Spring将扫描同一个包中的所有类以及所有子包中的注释/bean,如@Component..或@Repository。 确保您的存储库类位于同一个包或子包中,而不是同级包中。否则,将无法找到它,或者您必须修改ComponentScan。 那就应该找到了。
虽然其他人是对的,您可以在@Repository上省略@Component,但实际的错误是找不到@Bean

@SpringBootApplication主类上的注释包括@ComponentScan,这意味着Spring将扫描同一个包中的所有类以及所有子包中的注释/bean,如@Component..或@Repository。 确保您的存储库类位于同一个包或子包中,而不是同级包中。否则,将无法找到它,或者您必须修改ComponentScan。 那就应该找到了。
在Application.java类中的ComponentScan注释中,添加包com.ensa.repository。另外,从存储库中删除组件注释。在Application.java类中的ComponentScan注释中,添加包com.ensa.repository。另外,从存储库中删除组件注释。我在主类中添加了@EnableJPArepositorycom.ensa.repository,出现相同错误。我在主类中添加了@EnableJPArepositorycom.ensa.repository,出现相同错误。
public interface ItemRepository extends JpaRepository<Item, Long> {

}
@Autowired
private ItemRepository itemRepository;
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {

}