当我启动我的 spring-boot 应用程序时,我收到以下消息:
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.
我在与此问题相关的其他帖子中读到的是我必须配置注释@ComponentScan,但它不起作用
主要类(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
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 并且应用程序应该运行
最佳答案
1) 添加 @Repository
注解,以便将 DAO 作为 bean 加载到 spring 上下文中:
@Repository
public class UserDAO implements IUserDAO{
2) 就在旁边..您很可能还应该在其中注入(inject) EntityManager
:
@PersistenceContext
private EntityManager em;
关于java - 考虑在您的配置中定义类型为 'com.gisapp.gisapp.dao.IUserDAO' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55675934/