问题描述
@EntityScan("modele")
Test
并不是说真的是Spring Bean,而是 JPA Entity
。@ComponentScan
验看@Configuration
,@Component
,@Service
和@Repository
,@Controller
和@RestController
。@EntityScan
将寻找实体。
- Application.java在你的包的根:
com.domain.project
; - 您的存储库在
com.domain.project.dao
; - 您在下的实体
com.domain.project.domain
。
然后,您将不需要@EntityScan
,@ComponentScan
而@EnableJpaRepositories
SpringBoot将仅提取com.domain.project中发现的所有内容。*
解决方法
下面是跟踪:
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不被视为一个实体有什么关系?
这是我的项目:
架构: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
我将不胜感激。谢谢!