简体   繁体   English

Spring @Autowire 失败,没有找到类型的合格 bean 依赖错误

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

Suddenly i cant @Autowired any more beans.突然我不能@Autowired 更多的豆子了。 I created a Report entity我创建了一个报告实体

Report.java:报告.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 {
...
}

Then i created a DAO instance for this entity然后我为这个实体创建了一个 DAO 实例

ReportDAO.java:报告DAO.java:

package com.prime.technology.dao;

import java.util.List;

import com.prime.technology.entity.Report;

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

Then i created the implementation of this interface然后我创建了这个接口的实现

ReportDAOImpl.java:报告DAOImpl.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() {
        ...
    }
...
}

Then i created a service layer然后我创建了一个服务层

ReportService.java:报告服务.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: 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();
    }
...

Finally i @Autowired the ReportService interface in the Controller.最后我@Autowired Controller 中的 ReportService 接口。 Everything works perfect until here.直到这里一切都很完美。 After this i need to repeat the process for a different entity which is very similar, but i received an error.在此之后,我需要为一个非常相似的不同实体重复该过程,但我收到了一个错误。 After hours of debugging i decided to copy each file that was mentioned above and to add a "2" at the end of each name.经过数小时的调试,我决定复制上面提到的每个文件,并在每个名称的末尾添加一个“2”。

Controller.java: 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;
...
}

Finally the error:最后报错:

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)
    ...

I mention that i use full Java Configuration (no xml).我提到我使用完整的 Java 配置(无 xml)。 For other beans its working perfectly i assume i dont have any problem in the configuration.对于其他 bean,它工作得很好,我假设我在配置中没有任何问题。 Thanks in advance!提前致谢! i look forward for your advices!我期待您的建议!

EDIT: Report2.java:编辑:报告2.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:报告DAO2.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:报告DAOImpl2.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: 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: 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();
    }
...
}

The problem got solved by itself i suppose.我想这个问题自己解决了。

What i did:我做了什么:

  • I annotated the ReportServiceImpl2.java class with @Service("test2")我用 @Service("test2") 注释了 ReportServiceImpl2.java class
  • I created a new ReportService implementation called ReportServiceImpl3.java我创建了一个名为 ReportServiceImpl3.java 的新 ReportService 实现
  • I annotated this class with @Service("test3")我用 @Service("test3") 注释了这个 class
  • In the controller i used @Qualifier("test2") annotation After i did this it works.在 controller 中,我使用了 @Qualifier("test2") 注释在我这样做之后它可以工作。 Moreover i deleted ReportServiceImpl3.java and i came back to the previous state of the code and it works now.此外,我删除了 ReportServiceImpl3.java 并且我回到了之前的 state 代码,它现在可以工作了。

I suppose it was an IDE(Eclipse) problem which got solved by itself.我想这是一个自行解决的IDE(Eclipse)问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自动连接@service类? 找不到依赖类型的合格Bean - How to autowire @service class ? No qualifying bean of type found for dependency 没有为依赖项找到UserRepository类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者 - No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 未找到依赖项的类型为[PATHTOCLASS]的合格Bean:至少应有1个符合此依赖项自动候选条件的Bean - No qualifying bean of type [PATHTOCLASS] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency Crudrepository - 没有为依赖找到符合条件的 bean:预计至少有 1 个 bean 有资格作为这个依赖的自动装配候选者 - Crudrepository - No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 找不到依赖类型的合格Bean - No qualifying bean of type found for dependency NoSuchBeanDefinitionException: 没有为依赖找到 [Repository] ​​类型的合格 bean:预计至少有 1 个符合自动装配条件的 bean - NoSuchBeanDefinitionException: No qualifying bean of type [Repository] found for dependency: expected at least 1 bean which qualifies as autowire 在Spring Boot单表中找不到依赖项类型的合格Bean - No qualifying bean of type found for dependency in Spring Boot single table Spring Data JPA没有类型的限定bean ...找到依赖项 - Spring Data JPA No qualifying bean of type … found for dependency 春季错误:没有找到依赖项类型为[org.hibernate.SessionFactory]的合格bean - Spring error: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency Spring Data JPA-未找到符合条件的Bean - Spring data JPA - No qualifying bean found for dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM