Below is the trace :
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
From my understanding the root error is Not a managed type: class modele.Test, which would have something to do with Test not being recognized as an entity ?
Here's my project :
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 {
/**
* 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
I'd appreciate any help. Thanks!
解决方案
With your current setup, you need to add
@EntityScan("modele")
Test isn't really a Spring Bean per say, it's a JPA Entity. @ComponentScan looks for @Configuration, @Component, @Service and @Repository, @Controller and @RestController. @EntityScan will look for Entities.
Your configuration would be much easier if you'd move :
Application.java at the root of your packages: com.domain.project;
your repositories under com.domain.project.dao;
your entities under com.domain.project.domain.
Then, you wouldn't need @EntityScan, @ComponentScan and @EnableJpaRepositories, SpringBoot will just pickup everything found in com.domain.project.*