下面是跟踪:
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不被视为一个实体有什么关系?
Not a managed type: class modele.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
我将不胜感激。谢谢!
使用当前设置,您需要添加
@EntityScan("modele")
Test并不是说真的是Spring Bean,而是 JPA Entity 。@ComponentScan验看@Configuration,@Component,@Service和@Repository,@Controller和@RestController。@EntityScan将寻找实体。
Test
@ComponentScan
@Configuration
@Component
@Service
@Repository
@Controller
@RestController
@EntityScan
如果您进行以下操作,您的配置将更加容易:
com.domain.project
com.domain.project.dao
com.domain.project.domain
然后,您将不需要@EntityScan,@ComponentScan而@EnableJpaRepositoriesSpringBoot将仅提取com.domain.project中发现的所有内容。*
@EnableJpaRepositories