When I start my spring-boot app I have this message:
APPLICATION FAILED TO START
Description:
Field userDAO in com.gisapp.services.impl.UserService required a bean of type 'com.gisapp.gisapp.dao.IUserDAO' that could not be found.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.gisapp.gisapp.dao.IUserDAO' in your configuration.
What I have read in other post related to this problem is that I have to configure the annotation @ComponentScan, but it does not work
MAIN CLASS:
package com.gisapp.gisapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.gisapp")
public class GisappApplication {
public static void main(String[] args) {
SpringApplication.run(GisappApplication.class, args);
}
}
SERVICE CLASS
@Service
public class UserService implements IUserService {
@Autowired
IUserDAO userDAO;
@Override
@Transactional(readOnly=true)
public Object login() {
return userDAO.login();
}
}
- UserDAO
package com.gisapp.gisapp.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.gisapp.gisapp.dao.IUserDAO;
import com.gisapp.models.entity.User;
public class UserDAO implements IUserDAO{
@Override
public Object login() {
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM User");
EntityManager em = null;
Query q = em.createNativeQuery(query.toString());
List<User> result=q.getResultList();
return result;
}
}
IUserDAO should be recognised as a bean and the app should run
1) Add a @Repository
annotation in order for the DAO to be loaded as a bean into the spring context:
@Repository
public class UserDAO implements IUserDAO{
2) Just on the side.. you should most likely inject the EntityManager
in it also:
@PersistenceContext
private EntityManager em;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments