创建名为'XXX'的bean时出错:通过字段'XXX'表示的不满意的依赖关系 [英] Error creating bean with name 'XXX: Unsatisfied dependency expressed through field 'XXX'

查看:7101
本文介绍了创建名为'XXX'的bean时出错:通过字段'XXX'表示的不满意的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是trace:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'testController'的bean时出错:Unsatisfied dependency通过'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



根据我的理解,根错误是不是托管类型:class modele.Test ,这与测试未被识别为实体有关吗?



这是我的项目:



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



Application.java

  @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扩展CrudRepository< Test,Long> {

/ **
*此方法将通过电子邮件在数据库中找到User实例。
*请注意,此方法未实现,其工作代码将由Spring Data JPA的签名自动生成
*。
* /
public Test findByEmail(String email);

$ b $ / code>

Test.java

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

//自动生成的id(对于db中的每个用户都是唯一的)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私有长ID;

@NotNull
私人字符串电子邮件;

@NotNull
私人字符串名称;

//公共方法

public Test(){
}

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

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

我很感激任何帮助。感谢!

解决方案

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

  @EntityScan(modele)

Test 实际上并不是一个Spring Bean,它是一个 JPA实体 @ComponentScan 寻找 @Configuration @Component @Service @Repository @Controller @RestController @EntityScan 将查找实体。



您可以阅读以下内容: @EntityScan和@ComponentScan之间的区别


如果您愿意,您的配置会变得更容易: Application.java位于你的包的根目录下: com.domain.project ;

  • com下的存储库.domain.project.dao ;

  • com.domain.project.domain 下的实体



  • 然后,您不需要 @EntityScan @ComponentScan @EnableJpaRepositories ,SpringBoot将会取得com.domain.project中的所有内容。*


    Below is the trace :

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class modele.Test

    ...

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testDAO': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class modele.Test

    ...

    Caused by: java.lang.IllegalArgumentException: Not a managed type: class modele.Test

    From my understanding the root error is Not a managed type: class modele.Test, which would have something to do with Test not being recognized as an entity ?

    Here's my project :

    Architecture : http://imgur.com/a/2xsI4

    Application.java

    @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
    

    I'd appreciate any help. Thanks!

    解决方案

    With your current setup, you need to add

    @EntityScan("modele")
    

    Test isn't really a Spring Bean per say, it's a JPA Entity. @ComponentScan looks for @Configuration, @Component, @Service and @Repository, @Controller and @RestController. @EntityScan will look for Entities.

    You can read this: Difference between @EntityScan and @ComponentScan

    Your configuration would be much easier if you'd move :

    • Application.java at the root of your packages: com.domain.project;
    • your repositories under com.domain.project.dao;
    • your entities under com.domain.project.domain.

    Then, you wouldn't need @EntityScan, @ComponentScan and @EnableJpaRepositories, SpringBoot will just pickup everything found in com.domain.project.*

    这篇关于创建名为'XXX'的bean时出错:通过字段'XXX'表示的不满意的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆