Java不满足的依赖异常,创建名为'XXX'的bean时出错:通过字段'XXX'表示的不满意的依赖关系...

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 :

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 {

/**

* 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.

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.*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值