错误创建名称为豆“XXX:不满意依赖通过现场表示 'XXX'

问题描述:

下面是跟踪:错误创建名称为豆“XXX:不满意依赖通过现场表示 'XXX'

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,这与测试未被识别为实体有关吗?

这里是我的项目:

架构: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 

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

+0

请注意,如果将应用程序放在“dao”,“modele”和“boot”的父包中,那么这4个最后的注释是无用的。 Spring Boot将基于此自动应用合理的默认值。 –

+0

是的,但我的代码并非如此。尽管我现在已经改变了! – Chuck

根据您目前的设置,你需要添加

@EntityScan("modele") 

Test是不是真的每说一个Spring bean,这是一个JPA实体@ComponentScan的搜索结果@Configuration@Component@Service@Repository@Controller@RestController@EntityScan将查找实体。

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

您的配置会容易得多,如果你想移动:

  • Application.java在你的包的根:com.domain.project;
  • 您的知识库在com.domain.project.dao之下;
  • 您的实体根据com.domain.project.domain

然后,你就不需要@EntityScan@ComponentScan@EnableJpaRepositories,SpringBoot只会皮卡一切com.domain.project找到。*

+0

谢谢,修改了架构,正如您在其中解释的那样。 – Chuck