Spring @Autowire 失败并显示 No qualifying bean of type found for dependency 错误

Spring @Autowire fails with No qualifying bean of type found for dependency error

突然间我再也不能@Autowired了。 我创建了一个报告实体

Report.java :

package com.prime.technology.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="report")
public class Report {
...
}

然后我为这个实体创建了一个 DAO 实例

ReportDAO.java :

package com.prime.technology.dao;

import java.util.List;

import com.prime.technology.entity.Report;

public interface ReportDAO {
    
    public List<Report> getReports();
...
}

然后我创建了这个接口的实现

ReportDAOImpl.java :

package com.prime.technology.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.prime.technology.entity.Report;

@Repository
public class ReportDAOImpl implements ReportDAO {

    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public List<Report> getReports() {
        ...
    }
...
}

然后我创建了一个服务层

ReportService.java :

package com.prime.technology.service;

import java.util.List;

import com.prime.technology.entity.Report;

public interface ReportService {

    public List<Report> getReports();
...
}

ReportServiceImpl.java :

package com.prime.technology.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prime.technology.dao.ReportDAO;
import com.prime.technology.entity.Report;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDAO reportDAO;
    
    @Autowired
    private OrderService orderService;
    
    @Override
    @Transactional
    public List<Report> getReports() {
        return reportDAO.getReports();
    }
...

最后我@Autowired 控制器中的 ReportService 接口。一切都很完美,直到这里。 在此之后,我需要为另一个非常相似的实体重复该过程,但我收到了一个错误。 经过几个小时的调试,我决定复制上面提到的每个文件,并在每个名称的末尾添加一个“2”。

Controller.java :

package com.prime.technology.controller;

import java.security.Principal;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.prime.technology.entity.Report;
import com.prime.technology.entity.User;
import com.prime.technology.service.ReportService;
import com.prime.technology.service.ReportService2;
import com.prime.technology.service.UserService;


@Controller
@RequestMapping("/HR")
public class HrController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ReportService reportService;

    @Autowired
    private ReportService2 reportService2;
...
}

最后报错:

SEVERE: Servlet [dispatcher] in web application [/Prime-Technology] threw load() exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.prime.technology.service.ReportService2' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1504)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
    ...

Mar 19, 2021 12:35:41 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 19, 2021 12:35:41 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'dispatcher'
Mar 19, 2021 12:35:41 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'dispatcher'
Mar 19, 2021 12:35:43 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.1.6.Final
Mar 19, 2021 12:35:44 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 2856 ms
Mar 19, 2021 12:35:44 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Mar 19, 2021 12:35:44 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [19887] milliseconds
Mar 19, 2021 12:35:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Prime-Technology] threw exception
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    ...

我提到我使用完整的 Java 配置(没有 xml)。对于其他 bean,它工作得很好,我假设我在配置中没有任何问题。 提前致谢!我期待您的建议!

编辑: Report2.java :

package com.prime.technology.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="old-report")
public class Report2 {
...
}

ReportDAO2.java :

package com.prime.technology.dao;

import java.util.List;

import com.prime.technology.entity.Report2;

public interface ReportDAO2 {
    
    public List<Report2> getReports();
...
}

ReportDAOImpl2.java :

package com.prime.technology.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.prime.technology.entity.Report2;

@Repository
public class ReportDAOImpl2 implements ReportDAO2 {

    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public List<Report2> getReports() {
        ...
    }
...
}

ReportService2.java :

package com.prime.technology.service;

import java.util.List;

import com.prime.technology.entity.Report2;

public interface ReportService2 {

    public List<Report2> getReports();
...
}

ReportServiceImpl2.java :

package com.prime.technology.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prime.technology.dao.ReportDAO2;
import com.prime.technology.entity.Report2;

@Service
public class ReportServiceImpl2 implements ReportService2 {

    @Autowired
    private ReportDAO2 reportDAO;
    
    @Autowired
    private OrderService orderService;
    
    @Override
    @Transactional
    public List<Report2> getReports() {
        return reportDAO.getReports();
    }
...
}

我想问题已经自行解决了。

我做了什么:

  • 我用 @Service("test2")
  • 注释了 ReportServiceImpl2.java class
  • 我创建了一个名为 ReportServiceImpl3.java
  • 的新 ReportService 实现
  • 我用 @Service("test3")
  • 注释了这个 class
  • 在控制器中我使用了@Qualifier("test2") 注解 在我这样做之后它起作用了。此外,我删除了 ReportServiceImpl3.java,然后又回到了以前的代码状态,现在可以正常工作了。

我想这是一个 IDE(Eclipse) 问题,它自己解决了。