小编典典

创建名称为“ XXX”的bean时出错:通过字段“ XXX”表示的依赖关系未得到满足

spring-boot

下面是跟踪:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“
testController”的bean时出错:通过字段“
testDao”表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为’testDAO’的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:不是托管类型:class
modele.Test

由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为’testDAO’的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:不是托管类型:class
modele.Test

原因:java.lang.IllegalArgumentException:不是托管类型:class modele.Test

根据我的理解,根本的错误是Not a managed type: class modele.Test,与Test不被视为一个实体有什么关系?

这是我的项目:

架构:http//imgur.com/a/2xsI4

应用程序

@SpringBootApplication
@ComponentScan("boot")
@ComponentScan("dao")
@ComponentScan("modele")
@EnableJpaRepositories("dao")
public class Application {

    public static void main (String[] args){
        SpringApplication.run(Application.class, args);
    }

}

TestDAO.java

@Transactional
public interface TestDAO extends CrudRepository<Test, Long > {

  /**
   * This method will find an User instance in the database by its email.
   * Note that this method is not implemented and its working code will be
   * automagically generated from its signature by Spring Data JPA.
   */
  public Test findByEmail(String email);

}

Test.java

@Entity
@Table(name = "test")
public class Test {

    // An autogenerated id (unique for each user in the db)
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotNull
    private String email;

    @NotNull
    private String name;

    // Public methods

    public Test() {
    }

    public Test(long id) {
        this.id = id;
    }

    public Test(String email, String name) {
        this.email = email;
        this.name = name;
    }
//setters and getters

我将不胜感激。谢谢!


阅读 3598

收藏
2020-05-30

共1个答案

小编典典

使用当前设置,您需要添加

@EntityScan("modele")

Test并不是说真的是Spring Bean,而是 JPA Entity
@ComponentScan验看@Configuration@Component@Service@Repository@Controller@RestController@EntityScan将寻找实体。

如果您进行以下操作,您的配置将更加容易:

  • Application.java在你的包的根:com.domain.project;
  • 您的存储库在com.domain.project.dao;
  • 您在下的实体com.domain.project.domain

然后,您将不需要@EntityScan@ComponentScan@EnableJpaRepositoriesSpringBoot将仅提取com.domain.project中发现的所有内容。*

2020-05-30