java - 来自抽象 T 类型类的依赖注入(inject)

标签 java spring dependency-injection

我已经在代码中配置了Oracle和Postgres数据库。由于 oracle 和 Postgres 在设置 boolean 值等方面存在差异。我想配置 2 个不同的类(PostgresSetter 和 OracleSetter),它们根据数据库类型设置值。在运行时开始时,我想根据驱动程序类型注入(inject)数据库的setter类。目前,我注释掉了未使用的数据库,但我想以编程方式执行此操作。

public abstract class DBSetter<T> /*implements DBSetter this interface added later*/{
    public static void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {
        if (data != null) {
            statement.setBoolean(index, data);
        } else {
            //if psql
            statement.setNull(index, Types.BOOLEAN);
            //if oracle
            statement.setNull(index, Types.INTEGER);
        }
        ...//other functions
    }
}

上面是将 boolean 值设置为准备好的语句的类。我想将其分成 2 部分,如下所示。

public interface DBSetter {

    static void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {}

}

public class OracleSetter implements DBSetter {

    public static void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {
        if (data != null) {
            statement.setBoolean(index, data);
        } else {
            statement.setNull(index, Types.INTEGER);
        }
    }
}

public class PostgresSetter implements DBSetter {

    public static void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {
        if (data != null) {
            statement.setBoolean(index, data);
        } else {
            statement.setNull(index, Types.BOOLEAN);
        }
    }
}

我创建了一个接口(interface)并创建了 2 个名为 OracleSetter 和 PostgreSetter 的新类并实现了该接口(interface)。尽管如此,函数 setParameter 仍无法识别。如何根据 if 子句在启动时注入(inject) Setter?

最佳答案

我假设您正在使用 Spring 框架进行依赖注入(inject),因此您将使用 @Autowired 注入(inject)您的 bean,在您的情况下,您尝试注入(inject)的接口(interface)是 DBSetter.

我们将继续使用您的第二个代码片段,但在删除静态方法之后:

public interface DBSetter {

    void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception;

}

public class OracleSetter implements DBSetter {

    public void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {
        if (data != null) {
            statement.setBoolean(index, data);
        } else {
            statement.setNull(index, Types.INTEGER);
        }
    }
}

public class PostgresSetter implements DBSetter {

    public void setParameter(PreparedStatement statement, int index, Boolean data)
            throws Exception {
        if (data != null) {
            statement.setBoolean(index, data);
        } else {
            statement.setNull(index, Types.BOOLEAN);
        }
    }
}

并且您需要使用如下配置类来配置 bean 注入(inject):

@Configuration
public class DBSetterConfiguration {
      @Bean
      public DBSetter configureDBSetter() {
            if(isPsql()) { // try to implement isPsql yourself, the method to check if you are using Postgres or not
               return new PostgresSetter();
            }
            else {
                return new OracleSetter();
            }
      }
}

最后使用 @Autowired 调用 DBSetter:

@Autowired
DBSetter dbSetter;

关于java - 来自抽象 T 类型类的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69026842/

相关文章:

c# - IOC : Wiring up dependencies on event handlers

java - GUI 变得杂乱

java - Spring JPA 查询示例不返回结果

spring - 如何将 OAuth2 session 存储到数据库中并在 Spring Boot 服务器之间共享

java - 如何通过 Java 配置设置 Spring 原型(prototype) bean 的属性?

c# - ASP.NET Core 中的 IServiceProvider

c# - 管理一次性类依赖注入(inject)

java - Groovy:替换捕获组的惯用方法

java - 如何在 Apache Tomcat Tribes 集群中启用成员身份验证

java - 字符串比较中文字的正确位置是什么?