以下是追踪:
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
,这与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
我很感激任何帮助。谢谢!
答案 0 :(得分:1)
使用当前设置,您需要添加
for
@EntityScan("modele")
并不是真正的Spring Bean,它是 JPA Entity 。 Test
会查找@ComponentScan
,@Configuration
,@Component
和@Service
,@Repository
和@Controller
。 @RestController
会查找实体。
您可以阅读:Difference between @EntityScan and @ComponentScan
如果您搬家,您的配置会更容易:
@EntityScan
; com.domain.project
; com.domain.project.dao
下的实体。然后,您不需要com.domain.project.domain
,@EntityScan
和@ComponentScan
,SpringBoot将只提取com.domain.project中找到的所有内容。*