Reputation: 57
I'm working on a project on my sparetime, on this project I have been asked to work with Spring Boot. I'm not familiar with Spring Boot from before, and I have tried searching here on stackoverflow and Google, but I can't seem to understand how the different solutions for my problem works.
First of all, this is my error message:
Parameter 0 of constructor in com.project.example.controller.VerverController required a bean of type 'com.project.example.dao.VerverDao' that could not be found.
Action:
Consider defining a bean of type 'com.project.example.dao.VerverDao' in your configuration.
After some reading, I understood that this could be because of Spring Boot don't scan this class / interface. So I tried to add ComponentScan, with no luck.
I tried to convert VerverDao from interface to class and register a bean with bean annotation. Still no luck. I tried to move the VerverDao from the dao package to the root package (the same as the Main class) and still no luck.
So if someone could please help me understand the error message, it would make my day.
This is my structure:
This is my code:
Probably some of the annotation is wrong as well, since I'm not used to Spring Boot, in advance thanks for correcting me on the use of these It is also worth mentioning that I'm using Lombok as well on this project.
Main.java
@ComponentScan("com.gambinomafia")
@SpringBootApplication
public class Main extends SpringBootServletInitializer {
public static void main(String[] args) {
new Main().configure(new SpringApplicationBuilder(Main.class)).run(args);
}
}
Configuration.java
@Component
public class Configuration extends ResourceConfig {
public Configuration() {
register(VerverResourceImpl.class, VerverResource.class);
register(VerverController.class);
register(VerverDao.class);
}
}
VerverResource.java
@Resource
@Path("verver")
public interface VerverResource {
@GET
@Path("/{userid}")
@Produces(MediaType.APPLICATION_JSON)
String getRefferingUrl(@PathParam("userid") int userid);
}
VerverResourceImpl.java
@AllArgsConstructor(onConstructor = @__({@Inject}))
public class VerverResourceImpl implements VerverResource {
private VerverController controller;
@Override
public String getRefferingUrl(int userid) {
log.info("Collection reciever url for user id: {}", userid);
return controller.getRefferingUrl(userid);
}
}
VerverController.java
@Controller
@AllArgsConstructor
public class VerverController {
private VerverDao ververDao;
public String getRefferingUrl(int userid) {
User user = ververDao.getUsername(userid);
if (user.getUsername().isEmpty()) {
throw new NotFoundException("Did not find any user with id " + userid);
}
return "<url-to-site>/?verv=" + user.getUsername();
}
}
VerverDao.java
@AllArgsConstructor
public class VerverDao {
@Value("spring.datasource.url")
private String dbcon;
public User getUsername(int userid) {
Jdbi jdbi = Jdbi.create(dbcon);
return jdbi.withHandle(handle -> handle.createQuery(
"SELECT id, brukernavn FROM t_user WHERE id = :id")
.bind("id", userid)
.mapToBean(User.class)
.findOnly());
}
}
Feel free to ask for more information, if there is missing some information in the topic. I will try to answer as fast as possible.
Upvotes: 1
Views: 1628
Reputation: 585
First at all, you're setting a @ComponentScan
with a wrong package in your Application class.
@ComponentScan("com.gambinomafia")
@SpringBootApplication
And, according to the picture, it has to be:
@ComponentScan("com.project.example")
@SpringBootApplication
EDIT: the use of @ComponentScan
by @M.Deinum
Because of the Application Class is in the root package and @SpringBootApplication
already contains @ComponentScan
, It isn't needed to declare @ComponentScan
.
Second, instead of use a DAO you can use Repositories. There is more information here: Spring Boot Repositories.
Third, to inject your objects you can use @Autowired
over the constructor.
@Controller
public class VerverController
private VerverDao ververDao;
@Autowired
public VerverController(VerverDao ververDao) {
this.ververDao = ververDao;
}
}
There is more information here.
Upvotes: 4
Reputation: 5283
Log clearly says
VerverController required a bean of type 'com.project.example.dao.VerverDao' that could not be found.
It means there is no bean of type com.project.example.dao.VerverDao
Add @Repository
on top of VerverDao
to resolve the issue.
@Repository
@AllArgsConstructor
public class VerverDao {
}
Note: Ensure VerverDao is in component scanning path as per your package structure.
Upvotes: 1