【发布时间】:2017-05-15 11:51:09
【问题描述】:
下面是痕迹:
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“testController”的 bean 时出错:不满足的依赖关系 通过字段“testDao”表示;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“testDAO”的 bean:调用 init 方法失败; 嵌套异常是 java.lang.IllegalArgumentException: Not a managed 类型:类modele.Test
...
引起:org.springframework.beans.factory.BeanCreationException: 创建名为“testDAO”的 bean 时出错:调用 init 方法 失败的;嵌套异常是 java.lang.IllegalArgumentException: Not a 托管类型:类 modele.Test
...
原因:java.lang.IllegalArgumentException:不是托管类型: 类modele.Test
据我了解,根本错误是 Not a managed type: class modele.Test,这与 Test 未被识别为实体有关?
这是我的项目:
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
如果有任何帮助,我将不胜感激。谢谢!
【问题讨论】:
-
附带说明,如果您将应用程序放在“dao”、“modele”和“boot”的父包中,最后这 4 个注释将毫无用处。 Spring Boot 将基于此自动应用合理的默认值。
-
是的,但在我的代码中并非如此。不过我现在已经改变了!
标签: hibernate spring-boot spring-data-jpa