自定义 StoredProcedure class 作为 Spring 组件并扩展 jdbc StoredProcedure

Custom StoredProcedure class as a Spring component and extends jdbc StoredProcedure

我有一个扩展 jdbc.StoredProcedure 的自定义存储过程 class 但我已经用 Spring @Component 注释了这个 class 以带来这个 class将 bean 放入 Spring 上下文中。

我为什么要这样做?

  1. 我想添加 spring-retry on execute 方法,该方法仅适用于 spring 组件
  2. 我想重用编译后的StoredProcedure,而不是每次都创建一个新的对象重新编译,这样我每次都可以重用编译后的StoredProcedure。

这种实现有什么问题吗? 这个基于 Spring 组件的 StoredProcedure 有什么问题吗?

例如:

    @Component
public class ExampleStoredProcedure extends StoredProcedure {
  @Autowired
  private DataSource dataSource;

  @Postconstruct
  public void init() {
     super.setDataSource(dataSource);
     setSql("stored_procedure_name");
//TODO      declare parameters
     compile();
  }

  public void execute(){
    //Todo set all parameters to ParameterSource
    super.execute(parameterSource);
  }
}

尝试实现分层应用程序架构,在其中使用 spring 重试注释您的服务,如下例所示:

https://dzone.com/articles/spring-retry-way-to-handle-failures

这些服务方法可以定义事务边界并调用您的数据持久层方法,这些方法可以基于 spring 数据的标准化方法来调用存储过程和管理您的数据库连接等。

有关 Spring 和体系结构的更多信息,请参阅此简介:

https://www.petrikainulainen.net/software-development/design/understanding-spring-web-application-architecture-the-classic-way/