I am following this tutorial to test Hibernate before implementing it into my project : http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/
My problem is that this instruction requires UserDAO to be a bean :
@Autowired
private UserDao userDao;
Which it isn't ... ? I feel like I am missing something very important in the example provided by this website. I'm used to implements beans using implements Serializable
but it is not used in this scenario. How is it supposed to be considered as a bean ?
I'd appreciate very much if someone could explain to me what I am missing. This way of implementing a CRUD and more only using the interface and standard ways to declare functions looks very appealing. Thank you!
Edit : below is my code. The code provided with the tutorial does not work when I try to integrate it in my project, because of differences in the architecture of the project (that's my guess).
Architecture : https://i.stack.imgur.com/xcYVi.jpg
Application.java
@SpringBootApplication
@ComponentScan("boot")
@ComponentScan("dao")
@ComponentScan("modele")
@EnableJpaRepositories("org.utc.ai15")
public class Application {
public static void main (String[] args){
SpringApplication.run(Application.class, args);
}
}
TestDAO.java
@Transactional
@Component
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);
}
I think that is everything involved in the problem.
I've tried to add @Component
to TestDAO.java so it is scanned with @ComponentScan("dao")
but it doesn't work.
Here's the error :
Field testDao in boot.controller.TestController required a bean of type 'dao.TestDAO' that could not be found.
Edit 2 : Error was @EnableJpaRepositories("org.utc.ai15")
, correct declaration was @EnableJpaRepositories("dao")
(cf @Alex answer).