【问题标题】:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ordersServiceImpl'org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ordersServiceImpl”的bean时出错
【发布时间】:2021-09-18 13:23:42
【问题描述】:

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

所以,这是错误信息输出:

原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ordersServiceImpl”的bean时出错:通过字段“ordersDAO”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在创建 cl.duoc.proyectoInventario.dao.OrdersDAO 中定义的名称为“ordersDAO”的 bean 时出错,在 JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration 上声明的 @EnableJdbcRepositories 中定义:调用 init 方法失败;嵌套异常是 org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository cl.duoc.proyectoInventario.dao.OrdersDAO implements org.springframework.data.repository.query.QueryByExampleExecutor 但 JdbcRepositoryFactory 不支持 Query by Example! 原因:org.springframework.beans.factory.BeanCreationException:创建名称为“ordersDAO”的bean 时出错,该bean 定义在在JdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration 上声明的@EnableJdbcRepositories 中定义的cl.duoc.proyectoInventario.dao.OrdersDAO:调用init 方法失败;嵌套异常是 org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository cl.duoc.proyectoInventario.dao.OrdersDAO implements org.springframework.data.repository.query.QueryByExampleExecutor 但 JdbcRepositoryFactory 不支持 Query by Example! 原因:org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository cl.duoc.proyectoInventario.dao.OrdersDAO implements 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

【问题讨论】:

    标签: spring-boot hibernate spring-mvc javabeans


    【解决方案1】:

    你的查询有误,把订单改成Orders(实体类名)

    试试这个

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

    【讨论】:

    • 我已经按照您的建议更改了这些行,但仍然无法正常工作。谢谢! ``` @Data @Entity @Table(value="Orders") public class Orders implements Serializable { //... } ``` ``` @Query("SELECT o FROM Orders o WHERE o.status= :status ") ```
    • @Table(value="Orders") 让这个和以前一样并分享错误。
    • 我按照您的建议更新了表注释:``` @Data @Entity @Table(value="Orders") ``` 现在查询:``` Query("SELECT o FROM订单 o WHERE o.status= :status") ``` 但它仍然无法正常工作。我将在主帖中更新错误跟踪。谢谢!
    【解决方案2】:

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

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

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

    正确的库:

    import javax.persistence.Table;
    

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 2021-11-17
      • 2021-08-25
      • 2021-08-13
      • 2018-11-03
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多