当我启动我的弹簧启动应用程序时,我收到以下消息:
应用程序无法启动
描述:
Field userDAO in com.gisapp.services.impl.UserService require a 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();
}
}
- 用户道
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,应用程序应该运行
1( 添加一个@Repository
注释,以便将 DAO 作为 bean 加载到 spring 上下文中:
@Repository
public class UserDAO implements IUserDAO{
2(就在侧面。您很可能还应该在其中注入EntityManager
:
@PersistenceContext
private EntityManager em;