微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ordersServiceImpl”的 bean 时出错

如何解决org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ordersServiceImpl”的 bean 时出错

我是 Java SpringBoot 编程的新爱好者,我正在开发一些项目,我接下来将向您展示这个项目。 如果你能帮助我,我很感激,因为这让我很头疼。我尝试使用实体 Orders 中的参数/属性创建一个查询,以便稍后连接到带有 Thymeleaf 表达式的视图,但之前以某种方式连接失败。

所以,这是错误信息输出

引起:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ordeRSServiceImpl”的bean时出错:通过字段“ordersDAO”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration 上声明的 @EnableJdbcRepositories 中定义的 cl.duoc.proyectoInventario.dao.OrdersDAO 中定义的名称为“ordersDAO”的 bean 创建时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository cl.duoc.proyectoInventario.dao.OrdersDAO 实现了 org.springframework.data.repository.query.QueryByExampleExecutor 但 JdbcRepositoryFactory 不支持示例查询! 引起: org.springframework.beans.factory.BeanCreationException:创建名为 'ordersDAO' 的 bean 时出错,在 cl.duoc.proyectoInventario.dao.OrdersDAO 中定义,在 JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration 上声明的 @EnableJdbcRepositories 中定义:调用 init 方法失败;嵌套异常是 org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository cl.duoc.proyectoInventario.dao.OrdersDAO 实现了 org.springframework.data.repository.query.QueryByExampleExecutor 但 JdbcRepositoryFactory 不支持示例查询! 引起:org.springframework.data.repository.core.support.UnsupportedFragmentException:Repository cl.duoc.proyectoInventario.dao.OrdersDAO 实现了 org.springframework.data.repository.query.QueryByExampleExecutor 但 JdbcRepositoryFactory 不支持 Query by Example!>

PS:我为我的英语道歉

package com.example.proyectoInventario.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.Data;
import org.springframework.data.relational.core.mapping.Table;

@Data
@Entity
@Table(value="orders")
public class Orders implements Serializable {
    @Id // id 
    private Integer orderNumber;
    
    @Column
    private String orderDate;
    
    @Column
    private String requiredDate;
    
    @Column
    private String shippedDate;
    
    @Column
    private String status;
    
    @Column
    private String comments;
 
    @ManyToOne
    @JoinColumn(name="CustomerNumber")
    private Customers customer;
}

存储库/DAO

package com.example.proyectoInventario.dao;

import com.example.proyectoInventario.domain.Orders;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface OrdersDAO extends JpaRepository<Orders,Integer>{
    
    @Query("SELECT o FROM orders o WHERE o.status= :status")
    List<Orders> findByStatus(@Param ("status") String status);
}   

服务

package com.example.proyectoInventario.service;

import com.example.proyectoInventario.domain.Orders;
import java.util.List;

public interface OrdeRSService {
    
    public List<Orders> findByStatus(String status);
    
}

服务实施

package com.example.proyectoInventario.service;

import com.example.proyectoInventario.dao.OrdersDAO;
import com.example.proyectoInventario.domain.Orders;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrdeRSServiceImpl implements OrdeRSService{
    @Autowired
    private OrdersDAO ordersDAO;

    @Override
    @Transactional(readOnly=true)
    public List<Orders> findByStatus(String status) {
        return ordersDAO.findByStatus(status);
    }

控制器(我认为这里可能还有一些错误

package com.example.proyectoInventario.web;

import com.example.proyectoInventario.domain.Orders;
import com.example.proyectoInventario.service.OrdeRSService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@Slf4j
public class IndexController {

    String url = "";

    @Autowired
    private OrdeRSService ordeRSService;
    
     @GetMapping("/{status}")
    public List<Orders> lisTarordenesporStatus(@RequestParam String status){
        return ordeRSService.findByStatus(status);
    }

项目申请

package com.example.proyectoInventario;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan(basePackages="com.example.proyectoInventario")
@EnableJpaRepositories(basePackages="com.example.proyectoInventario.dao")
@EntityScan(basePackages="com.example.proyectoInventario.domain")
public class ProyectoInventarioApplication {

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

}

Application.properties

server.port= 8000
spring.main.banner-mode=off

# Base de datos
spring.jpa.database=MysqL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
spring.datasource.url=jdbc:MysqL://localhost/inventory?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=

#This line it was added by suggestions of Netbeans itself
spring.main.allow-bean-deFinition-overriding=true

解决方法

您的查询有误,将订单改成订单(实体类名)

试试这个

Query("SELECT o FROM Orders o WHERE o.status= :status")

,

终于找到了问题所在。为了解决这个问题,我正确替换了@Table Annotation 的导入库。

最初,我使用了这个 Spring 导入:

import org.springframework.data.relational.core.mapping.Table;

正确的库:

import javax.persistence.Table;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。