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

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

突然,我不能@自动连线更多的豆子。我创建了一个报表实体
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”。
控制器.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();
    }
...
}
x0fgdtte

x0fgdtte1#

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

相关问题