简体   繁体   中英

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

Suddenly i cant @Autowired any more beans. I created a Report entity

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 {
...
}

Then i created a DAO instance for this entity

ReportDAO.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:

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:

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();
    }
...

Finally i @Autowired the ReportService interface in the Controller. 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.

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). For other beans its working perfectly i assume i dont have any problem in the configuration. Thanks in advance! i look forward for your advices!

EDIT: 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();
    }
...
}

The problem got solved by itself i suppose.

What i did:

  • I annotated the ReportServiceImpl2.java class with @Service("test2")
  • I created a new ReportService implementation called ReportServiceImpl3.java
  • I annotated this class with @Service("test3")
  • In the controller i used @Qualifier("test2") annotation After i did this it works. Moreover i deleted ReportServiceImpl3.java and i came back to the previous state of the code and it works now.

I suppose it was an IDE(Eclipse) problem which got solved by itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM