spring@autowire失败,找不到符合依赖项错误类型的bean

goucqfw6  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(422)

突然,我不能@自动连线更多的豆子。我创建了一个报表实体
report.java文件:

  1. package com.prime.technology.entity;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. @Entity
  9. @Table(name="report")
  10. public class Report {
  11. ...
  12. }

然后我为这个实体创建了一个dao示例
reportdao.java文件:

  1. package com.prime.technology.dao;
  2. import java.util.List;
  3. import com.prime.technology.entity.Report;
  4. public interface ReportDAO {
  5. public List<Report> getReports();
  6. ...
  7. }

然后我创建了这个接口的实现
reportdaoimpl.java文件:

  1. package com.prime.technology.dao;
  2. import java.util.List;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.query.Query;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Repository;
  8. import com.prime.technology.entity.Report;
  9. @Repository
  10. public class ReportDAOImpl implements ReportDAO {
  11. @Autowired
  12. private SessionFactory sessionFactory;
  13. @Override
  14. public List<Report> getReports() {
  15. ...
  16. }
  17. ...
  18. }

然后我创建了一个服务层
reportservice.java报告服务:

  1. package com.prime.technology.service;
  2. import java.util.List;
  3. import com.prime.technology.entity.Report;
  4. public interface ReportService {
  5. public List<Report> getReports();
  6. ...
  7. }

reportserviceimpl.java报告服务:

  1. package com.prime.technology.service;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.TimeZone;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import com.prime.technology.dao.ReportDAO;
  11. import com.prime.technology.entity.Report;
  12. @Service
  13. public class ReportServiceImpl implements ReportService {
  14. @Autowired
  15. private ReportDAO reportDAO;
  16. @Autowired
  17. private OrderService orderService;
  18. @Override
  19. @Transactional
  20. public List<Report> getReports() {
  21. return reportDAO.getReports();
  22. }
  23. ...

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

  1. package com.prime.technology.controller;
  2. import java.security.Principal;
  3. import java.util.Calendar;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.ModelAttribute;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import com.prime.technology.entity.Report;
  13. import com.prime.technology.entity.User;
  14. import com.prime.technology.service.ReportService;
  15. import com.prime.technology.service.ReportService2;
  16. import com.prime.technology.service.UserService;
  17. @Controller
  18. @RequestMapping("/HR")
  19. public class HrController {
  20. @Autowired
  21. private UserService userService;
  22. @Autowired
  23. private ReportService reportService;
  24. @Autowired
  25. private ReportService2 reportService2;
  26. ...
  27. }

最后是错误:

  1. SEVERE: Servlet [dispatcher] in web application [/Prime-Technology] threw load() exception
  2. 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)}
  3. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1504)
  4. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
  5. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
  6. ...
  7. Mar 19, 2021 12:35:41 PM org.apache.jasper.servlet.TldScanner scanJars
  8. 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.
  9. Mar 19, 2021 12:35:41 PM org.apache.catalina.core.ApplicationContext log
  10. INFO: Initializing Spring DispatcherServlet 'dispatcher'
  11. Mar 19, 2021 12:35:41 PM org.springframework.web.servlet.FrameworkServlet initServletBean
  12. INFO: Initializing Servlet 'dispatcher'
  13. Mar 19, 2021 12:35:43 PM org.hibernate.validator.internal.util.Version <clinit>
  14. INFO: HV000001: Hibernate Validator 6.1.6.Final
  15. Mar 19, 2021 12:35:44 PM org.springframework.web.servlet.FrameworkServlet initServletBean
  16. INFO: Completed initialization in 2856 ms
  17. Mar 19, 2021 12:35:44 PM org.apache.coyote.AbstractProtocol start
  18. INFO: Starting ProtocolHandler ["http-nio-8080"]
  19. Mar 19, 2021 12:35:44 PM org.apache.catalina.startup.Catalina start
  20. INFO: Server startup in [19887] milliseconds
  21. Mar 19, 2021 12:35:45 PM org.apache.catalina.core.StandardWrapperValve invoke
  22. SEVERE: Servlet.service() for servlet [jsp] in context with path [/Prime-Technology] threw exception
  23. java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
  24. at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
  25. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  26. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  27. ...

我提到我使用完整的java配置(没有xml)。对于其他bean,它工作得很好,我假设我在配置上没有任何问题。提前谢谢!我期待你的建议!
编辑:report2.java:

  1. package com.prime.technology.entity;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. @Entity
  9. @Table(name="old-report")
  10. public class Report2 {
  11. ...
  12. }

reportdao2.java文件:

  1. package com.prime.technology.dao;
  2. import java.util.List;
  3. import com.prime.technology.entity.Report2;
  4. public interface ReportDAO2 {
  5. public List<Report2> getReports();
  6. ...
  7. }

reportdaoimpl2.java文件:

  1. package com.prime.technology.dao;
  2. import java.util.List;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.query.Query;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Repository;
  8. import com.prime.technology.entity.Report2;
  9. @Repository
  10. public class ReportDAOImpl2 implements ReportDAO2 {
  11. @Autowired
  12. private SessionFactory sessionFactory;
  13. @Override
  14. public List<Report2> getReports() {
  15. ...
  16. }
  17. ...
  18. }

reportservice2.java报告服务:

  1. package com.prime.technology.service;
  2. import java.util.List;
  3. import com.prime.technology.entity.Report2;
  4. public interface ReportService2 {
  5. public List<Report2> getReports();
  6. ...
  7. }

reportserviceimpl2.java报告:

  1. package com.prime.technology.service;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.TimeZone;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import com.prime.technology.dao.ReportDAO2;
  11. import com.prime.technology.entity.Report2;
  12. @Service
  13. public class ReportServiceImpl2 implements ReportService2 {
  14. @Autowired
  15. private ReportDAO2 reportDAO;
  16. @Autowired
  17. private OrderService orderService;
  18. @Override
  19. @Transactional
  20. public List<Report2> getReports() {
  21. return reportDAO.getReports();
  22. }
  23. ...
  24. }
x0fgdtte

x0fgdtte1#

我想这个问题自己解决了。
我所做的:
我用@service(“test2”)注解了reportserviceinmpl2.java类
我创建了一个新的reportservice实现,名为reportserviceimpl3.java
我用@service(“test3”)注解了这个类
在控制器中,我使用了@qualifier(“test2”)注解,在我这样做之后,它就工作了。此外,我删除了reportserviceinmpl3.java,回到了代码的前一个状态,现在可以工作了。
我想这是一个ide(eclipse)问题,它自己解决了。

相关问题