简体   繁体   中英

Java Spring Boot Application: Error creating bean with name, expected at least 1 bean which qualifies as autowire candidate for this dependenc

I'm new to Spring boot, I have developed my first application which received data from XML and queries Oracle DB, JPA.

I'm getting the following error:

[main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingRestController': Unsatisfied dependency expressed through field 'inlandPricingService': Error creating bean with name 'inlandPricingServiceImpl': Unsatisfied dependency expressed through field 'inlandPricingDAO': Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingServiceImpl': Unsatisfied dependency expressed through field 'inlandPricingDAO': Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined;
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inlandPricingDAO': Cannot create inner bean '(inner bean)#1386313f' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager';
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1386313f': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inlandPricingRestController': Unsatisfied dependency expressed through field 'inlandPricingService': No qualifying bean of type [com.inlandpricinglocationauditor.dao.InlandPricingService] found for dependency [com.inlandpricinglocationauditor.dao.InlandPricingService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.inlandpricinglocationauditor.dao.InlandPricingService] found for dependency [com.inlandpricinglocationauditor.dao.InlandPricingService]:

Here're my project files:

Application.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

InlandPricingRestController.java:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InlandPricingRestController {

    @Autowired
    InlandPricingService inlandPricingService;

    @RequestMapping(path = "/inlandlocation", method = RequestMethod.GET)
    public String hello() {
        return "Hello Mudassir!";
    }

    @RequestMapping(path = "/inlandlocationPOSTtest1", method = RequestMethod.POST, consumes = { "application/xml" })
    public InlandDBStructure postMethodXMLreturnXML1(
            @RequestBody InlandDBStructure list) {

        InlandDBStructure InlandCY = list;
        System.out.println("SOP : post Method started . Name "+ InlandCY.geteffective_dt());
        return InlandCY;
    }

    @RequestMapping(path = "/inlandlocationReal", method = RequestMethod.POST, consumes = { "application/xml" })
    public String postMethodXMLreturnXML2(@RequestBody InlandDBStructure list) {

        InlandDBStructure InlandCY = list;
        System.out.println("SOP : post Method started . Name "+ InlandCY.geteffective_dt());
        return inlandPricingService.findByGeoIDEffectiveExpiryDate(InlandCY.gethub_geoid(), InlandCY.geteffective_dt(),InlandCY.getexpiry_dt());

    }

}

InlandDBStructure.java:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/*public class InlandDBStructure {

}
*/


@XmlRootElement(name = "InlandDBStructure")
public class InlandDBStructure {  
    private String hub_geoid;
    private String effective_dt;
    private String expiry_dt;
    private String last_updated_dt;

    public InlandDBStructure() {}  
    public InlandDBStructure(String hUB_GEOID, String eFFECTIVE_DT, String eXPIRY_DT, String lAST_UPDATED_BY) {  
        super();  
        this.hub_geoid = hUB_GEOID;  
        this.effective_dt = eFFECTIVE_DT;  
        this.expiry_dt = eXPIRY_DT;
        this.last_updated_dt = lAST_UPDATED_BY;
    }
    @XmlAttribute 
    public String gethub_geoid() {
        return hub_geoid;
    }
    public void sethub_geoid(String hUB_GEOID) {
        hub_geoid = hUB_GEOID;
    }
    @XmlElement 
    public String geteffective_dt() {
        return effective_dt;
    }
    public void seteffective_dt(String eFFECTIVE_DT) {
        effective_dt = eFFECTIVE_DT;
    }
    @XmlElement 
    public String getexpiry_dt() {
        return expiry_dt;
    }
    public void setexpiry_dt(String eXPIRY_DT) {
        expiry_dt = eXPIRY_DT;
    }
    @XmlElement 
    public String getlast_updated_dt() {
        return last_updated_dt;
    }
    public void setlast_updated_dt(String lAST_UPDATED_BY) {
        last_updated_dt = lAST_UPDATED_BY;
    }  


}

InlandPricingDAO.java:

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface InlandPricingDAO extends CrudRepository<InlandPricingDB, Long>{

    @Query(value="SELECT HUB_GEOID from INLAND_CY where HUB_GEOID=:hubgeoid AND EFFECTIVE_DT <= TO_DATE(:effectivedate,'dd-mm-yyyy') AND EXPIRY_DT >= TO_DATE(:exiprydate,'DD-MM-YYYY')",nativeQuery = true)
    String findByGeoIDEffectiveExpiryDate(@Param("hubgeoid") String hubgeoid,@Param("effectivedate") String effectivedate ,@Param("exiprydate") String exiprydate);


}

InlandPRicingDetailRepository.java:

public interface InlandPricingDetailRepository  extends JpaRepository<InlandPricingDB, Long>{

}

InlandPricingService.java:

public interface InlandPricingService {
    List<InlandPricingDB> getAllDetails();
    String findByGeoIDEffectiveExpiryDate(String hubgeoid, String effectivedate, String exiprydate);
}

InlandPricingServiceImpls.java:

@Service
public class InlandPricingServiceImpl implements InlandPricingService{

    public InlandPricingServiceImpl() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Autowired
    private DataSource dataSource;

    @Autowired
    private InlandPricingDAO inlandPricingDAO;

    @Autowired
    private InlandPricingDetailRepository inlandPricingDetailRepository;

    @Override 
    public String findByGeoIDEffectiveExpiryDate(String hubgeoid, String effectivedate, String exiprydate) {
        return inlandPricingDAO.findByGeoIDEffectiveExpiryDate(hubgeoid,effectivedate,exiprydate);
    }

     @Override               
        public List<InlandPricingDB> getAllDetails() {
            return inlandPricingDetailRepository.findAll();
        }

     public InlandPricingDetailRepository getInlandPricingDetailRepository() {
            return inlandPricingDetailRepository;
        }

        public void setInlandPricingDetailRepository(InlandPricingDetailRepository inlandPricingDetailRepository) {
            this.inlandPricingDetailRepository = inlandPricingDetailRepository;
        }

        public InlandPricingDAO getInlandPricingDAO() {
            return inlandPricingDAO;
        }

        public void setInlandPricingDAO(InlandPricingDAO inlandPricingDAO) {
            this.inlandPricingDAO = inlandPricingDAO;
        }       

        public DataSource getDataSource() {
            return dataSource;
        }

        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
}

InlandPricingDB.java:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.springframework.data.annotation.Id;


/**
 * The persistent class for the INLANCY_CY VIP database table.
 * 
 */

@Entity
@Table(name = "INLAND_CY")
public class InlandPricingDB {
       @Id
        @Column(name = "HUB_GEOID")
        private String hub_geo_id;

        @Column(name = "EFFECTIVE_DT")
        private Date effective_dt;

        @Column(name = "EXPIRY_DT")
        private Date expitry_dt;

        @Column(name = "LAST_UPDATED_BY")
        private String last_updated_by;

        public String getHub_geo_id() {
            return hub_geo_id;
        }

        public void setHub_geo_id(String hub_geo_id) {
            this.hub_geo_id = hub_geo_id;
        }

        public Date getEffective_dt() {
            return effective_dt;
        }

        public void setEffective_dt(Date effective_dt) {
            this.effective_dt = effective_dt;
        }

        public Date getExpitry_dt() {
            return expitry_dt;
        }

        public void setExpitry_dt(Date expitry_dt) {
            this.expitry_dt = expitry_dt;
        }

        public String getLast_updated_by() {
            return last_updated_by;
        }

        public void setLast_updated_by(String last_updated_by) {
            this.last_updated_by = last_updated_by;
        }
}

Application.properties

server.port=9000

spring.unbilled.read.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.unbilled.read.datasource.username=test
spring.unbilled.read.datasource.password=test
spring.unbilled.read.datasource.driver-class-name=oracle.jdbc.OracleDriver

What am I doing wrong?

move the package of your InlandPricingDB.java class into the package of Application class for to make that inner.

sorry I can help you well in french ,i don't now well english

In spring boot your main class should be the outermost package and your beans can be in sub packages like controller, service, repository. I see that you are missing a default constructor on your controller. Spring boot has a ridiculous dependency on it. I am just guessing since I have got such an error in the past . However spring recommends constructor injection for must have beans. If you have too many must haves use setter injection with @Required annotations.

理想情况下,使用构造函数注入的bean注入应该可以工作。如果不起作用,请尝试使用@SpringBootApplication(scanBasePackages = {“ pkg1”,“ pkg2”}共享您的包/文件夹结构..这将有助于进一步分析。

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