I’m starting my first springboot project and all was okay untill I found this issue:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘controllerREST’: Unsatisfied dependency expressed through field ‘userDao’: Error creating bean with name ‘IUserDao’ defined in com.example.dao.IUserDao defined in @EnableJpaRepositories declared on WeatherStartingAppApplication: Not a managed type: class com.example.domain.User
I believe there’s something wrong with my folders structure:
packages
This is the main class:
<code>package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories(basePackages = "com.example.dao")
@EntityScan(basePackages = "com.example.domain")
@ComponentScan(basePackages = "com.example")
public class WeatherStartingAppApplication {
public static void main(String[] args) {
SpringApplication.run(WeatherStartingAppApplication.class, args);
<code>package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories(basePackages = "com.example.dao")
@EntityScan(basePackages = "com.example.domain")
@ComponentScan(basePackages = "com.example")
@SpringBootApplication
public class WeatherStartingAppApplication {
public static void main(String[] args) {
SpringApplication.run(WeatherStartingAppApplication.class, args);
}
}
</code>
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories(basePackages = "com.example.dao")
@EntityScan(basePackages = "com.example.domain")
@ComponentScan(basePackages = "com.example")
@SpringBootApplication
public class WeatherStartingAppApplication {
public static void main(String[] args) {
SpringApplication.run(WeatherStartingAppApplication.class, args);
}
}
ControllerREST:
<code>package com.example.controller;
import com.example.domain.User;
import com.example.dao.IUserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
public class ControllerREST {
private IUserDao userDao;
public String index(Model model){
Iterable<User> users = userDao.findAll();
model.addAttribute("users", users);
<code>package com.example.controller;
import com.example.domain.User;
import com.example.dao.IUserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@Slf4j
public class ControllerREST {
@Autowired
private IUserDao userDao;
@GetMapping("/")
public String index(Model model){
Iterable<User> users = userDao.findAll();
model.addAttribute("users", users);
return "index";
}
}
</code>
package com.example.controller;
import com.example.domain.User;
import com.example.dao.IUserDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@Slf4j
public class ControllerREST {
@Autowired
private IUserDao userDao;
@GetMapping("/")
public String index(Model model){
Iterable<User> users = userDao.findAll();
model.addAttribute("users", users);
return "index";
}
}
IUserDao:
<code>package com.example.dao;
import com.example.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
public interface IUserDao extends CrudRepository<User, Long> {
<code>package com.example.dao;
import com.example.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface IUserDao extends CrudRepository<User, Long> {
}
</code>
package com.example.dao;
import com.example.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface IUserDao extends CrudRepository<User, Long> {
}
User:
<code>package com.example.domain;
import java.io.Serializable;
import javax.persistence.*;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.IDENTITY)
// Default constructor required by JPA
public User(int dni, String name, String surname, String mail) {
public String toString() {
return "{" + dni + ' ' + name + ' ' + surname + ' ' + mail + "}";
<code>package com.example.domain;
import java.io.Serializable;
import javax.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int dni;
private String name;
private String surname;
private String mail;
public User() {
// Default constructor required by JPA
}
public User(int dni, String name, String surname, String mail) {
this.dni = dni;
this.name = name;
this.surname = surname;
this.mail = mail;
}
@Override
public String toString() {
return "{" + dni + ' ' + name + ' ' + surname + ' ' + mail + "}";
}
}
</code>
package com.example.domain;
import java.io.Serializable;
import javax.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int dni;
private String name;
private String surname;
private String mail;
public User() {
// Default constructor required by JPA
}
public User(int dni, String name, String surname, String mail) {
this.dni = dni;
this.name = name;
this.surname = surname;
this.mail = mail;
}
@Override
public String toString() {
return "{" + dni + ' ' + name + ' ' + surname + ' ' + mail + "}";
}
}
The pom file is okay, all dependencies needed are there, I’ve installed all requirements.