创建名称为' XXX的bean时出错:通过字段' XXX'表示不满意的依赖关系

时间:2016-12-31 13:34:54

标签: hibernate spring-boot spring-data-jpa

以下是追踪:

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

根据我的理解,根错误是Not a managed type: class modele.Test,这与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 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

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

1 个答案:

答案 0 :(得分:1)

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

for

@EntityScan("modele") 并不是真正的Spring Bean,它是 JPA Entity Test会查找@ComponentScan@Configuration@Component@Service@Repository@Controller@RestController会查找实体。

您可以阅读:Difference between @EntityScan and @ComponentScan

  

如果您搬家,您的配置会更容易:

  • Application.java位于软件包的根目录:@EntityScan;
  • 您在com.domain.project;
  • 下的存储库
  • com.domain.project.dao下的实体。

然后,您不需要com.domain.project.domain@EntityScan@ComponentScan,SpringBoot将只提取com.domain.project中找到的所有内容。*