Java Spring 引导应用程序:使用名称创建 bean 时出错,预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者

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

我是 Spring 引导新手,我开发了我的第一个应用程序,它从 XML 接收数据并查询 Oracle DB、JPA。

我收到以下错误:

[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]:

这是我的项目文件:

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

我做错了什么?

将您的 InlandPricingDB.java class 的包移动到应用程序 class 的包中以使其成为内部。

对不起,我可以用法语很好地帮助你,我现在英语不好

在 spring 引导中,您的主 class 应该是最外层的包,您的 bean 可以位于控制器、服务、存储库等子包中。我看到您在控制器上缺少默认构造函数。 Spring boot 对它有一个荒谬的依赖。我只是在猜测,因为我过去遇到过这样的错误。 但是 spring 建议对 must have beans 进行构造函数注入。如果你有太多必备品,请使用带有 @Required 注释的 setter 注入。

理想情况下,使用构造函数注入的 bean 注入应该 work.Try 这也是 @SpringBootApplication(scanBasePackages = {"pkg1", "pkg2"} 如果不工作分享你的 package/folder 结构 ..这将有助于进一步分析。