当我启动spring-boot应用程序时,我收到以下消息:
申请无法开始
说明:
com.gisapp.services.impl.UserService中的字段userDAO需要一个bean 找不到类型为“ com.gisapp.gisapp.dao.IUserDAO”的
注入点具有以下注释: -@ org.springframework.beans.factory.annotation.Autowired(required = true)
操作:
考虑在其中定义类型为“ com.gisapp.gisapp.dao.IUserDAO”的bean 您的配置。
我在其他与此问题相关的文章中读到的是,我必须配置注释@ComponentScan,但它不起作用
主要类别:
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
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应该被识别为bean,并且应用程序应该运行
答案 0 :(得分:2)
1):添加一个@Repository
批注,以将DAO作为bean加载到spring上下文中:
@Repository
public class UserDAO implements IUserDAO{
2)就在侧面。您还应该在其中注入EntityManager
@PersistenceContext
private EntityManager em;