1

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).

Chuck
  • 351
  • 1
  • 6
  • 20
  • userDao is a "repository" bean. Perhaps you should go through Spring Data JPA documentation ? https://docs.spring.io/spring-data/jpa/docs/current/reference/html. – alexbt Dec 31 '16 at 01:32
  • You tutorial mentions this: "With Spring Data JPA a DAO for your entity is simply created by extending the CrudRepository interface provided by Spring". In a nutshell, Spring Data gives you ready-to-use repositories just by creating an interface. You can then inject it as a bean and use the save/delete/find methods... – alexbt Dec 31 '16 at 01:33
  • Thanks for the reply. I understand a lot better what's going on. But I still can't create a bean of UserDAO. It's just not recognized as a bean. I've annoted with @EnableJpaRepositories, followed the tutorial which is identical to the steps defined in the doc. Do you have any idea what could go wrong? I'll post sample of my code tomorrow, but it's UserDAO is identical. – Chuck Dec 31 '16 at 02:10
  • I'd bet it has to do with your packages. You do have a `@SpringBootApplication` with a main method ? And that is where you annotated `@EnableJpaRepositories` ? Either you ensure UserDAO is **below** the SpringBootApplication (Spring Boot uses the main class as root to scan for beans) OR you define the the repositories package with `@EnableJpaRepositories(basePackages="org.my.pkg")` OR you add `@ScanPackages` to define additional packages for beans discovery (not just repositories). That's all just a guess (based on common mistake I've seen). – alexbt Dec 31 '16 at 02:17
  • Will check all that tomorrow. Many thanks again. – Chuck Dec 31 '16 at 02:22
  • Tried different things, none worked. My package is defined in my pom right ? If I have `org.test.project`, that is what I should use with `@EnableJpaRepositories` ? I am editing my question to post my code. – Chuck Dec 31 '16 at 12:48
  • I meant package as in "package com.yourdomain.xyz;" at the top of your classes. – alexbt Dec 31 '16 at 12:51
  • Oh that was it. It's scanning it as a bean now. I have another error now but I'll try to figure it out. Just in case you'd have an idea, I'm gonna edit the question. – Chuck Dec 31 '16 at 13:01
  • I suggest you add another question. Let this question be about "packages and bean not discovered" – alexbt Dec 31 '16 at 13:02
  • That might actually be better, yes. Thanks. – Chuck Dec 31 '16 at 13:08
  • I posted my question in another thread. Would you mind checking it out ? Here's the link : http://stackoverflow.com/questions/41408757/error-creating-bean-with-name-xxx-unsatisfied-dependency-expressed-through-fie Thank you – Chuck Dec 31 '16 at 14:37

1 Answers1

1

It is most probably due to bean discovery/package scanning.

Check the following:

  • Either make sure your UserDAO is below the SpringBootApplication (Spring Boot uses the main class as root to scan for beans)
  • or you define the the repositories package with @EnableJpaRepositories(basePackages="org.my.pkg")
  • or you add @ScanPackages to define additional packages for beans discovery (not just repositories).

By packages, I mean what you have at the top of your classes.

alexbt
  • 16,415
  • 6
  • 78
  • 87