我可以自动连接适配器类吗?

时间:2019-05-22 17:49:52

标签: java spring spring-boot singleton autowired

我创建了一个单例类,用作我们Influx数据库的适配器。基本上看起来像:

for (int i = 1; i < 6; i++)
{
    yield return new WaitForSeconds(1);
    GameObject rocketspawnblue = Instantiate(
            rocketblue, 
            new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))), 
            Quaternion.identity);
    SpriteRenderer rocketscolor1 = rocketspawnblue.GetComponent<SpriteRenderer>();
    //rocketscolor.color = colors[Random.Range(0,colors.Length)];
    rocketscolor1.color = Color.blue;

    GameObject rocketspawnred = Instantiate(
            rocketred, 
            new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))), 
            Quaternion.identity);
    SpriteRenderer rocketscolor2 = rocketspawnred.GetComponent<SpriteRenderer>();
    //rocketscolor.color = colors[Random.Range(0, colors.Length)];
    rocketscolor2.color = Color.red;
}

然后在另一个类中,我像这样使用它:

public class InfluxDBAdapter {
    // some private static final query Strings       

    private static InfluxDBAdapter adapter = null;
    private static InfluxDB influxDB;

    private InfluxDBAdapter() {}

    public static InfluxDBAdapter getInstance() {
        if (adapter == null) {
            adapter = new InfluxDBAdapter();
            influxDB = InfluxDBFactory.connect(URL, USERNAME, PWD);
            influxDB.query(new Query(CREATE_DB, DB_NAME));
            influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
            influxDB.setDatabase(DB_NAME);
        }
        return adapter;
    }

    // some more methods to utilize the database
}

所以这行得通,但是我正处于重构代码的过程中,我想知道是否可以简单地自动连接InfluxDBAdapter类和我目前正在做什么,并且仍然能达到相同的结果?

2 个答案:

答案 0 :(得分:3)

创建一个@Configuration类,它既构造InfluxDB也构造您的适配器。这样,您甚至可以利用Spring Boot属性支持。

@Configuration
public class InfluxDBConfiguration {

  @Bean
  public InfluxDB influxDB() {
    InfluxDB influxDB = InfluxDBFactory.connect(URL, USERNAME, PWD);
    influxDB.query(new Query(CREATE_DB, DB_NAME));
    influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
    influxDB.setDatabase(DB_NAME);
    return influxDB;
  }

  @Bean
  public InfluxDBAdapter influxDBAdapter(InfluxDB influxDB) {
    return new InfluxDBAdapter(influxDB);
  }
}

现在您的InfluxDBAdapter需要一个构造函数(用于依赖注入)来检索InfluxDB

public class InfluxDBAdapter {
    // some private static final query Strings       

    private InfluxDB influxDB;

    InfluxDBAdapter(InfluxDB influxDB) {
      this.influxDB=influxDB;
    }

    // some more methods to utilize the database
}

确保InfluXDBConfigurationInfluxDBAdapter在同一程序包中,以便可以调用默认的可见构造函数(默认可见的构造函数用于防止外部实例化)。

InflxuDBConfiguration中,您可以删除包含硬编码用户名等的static字段,并用对Environment的访问权限或使用带有注释的@ConfigurationProperties的类来替换它。与type safe properties合作。

@ConfigurationProperties(prefix="influxdb")
@Component
public class InfluxDBProperties {

    private String url = "default-url";
    private String dbName = "default-dbname";
    private String username = "default-user";
    private String password = "default-pwd";
    // Other properties of your liking;
    // getters & setters
}

现在,使用此InfluxDBProperties,您可以将influx.url=http://whatever添加到您的application.properties或个人资料特定的文件夹中,并将其externally configurable设置。您可以将其注入influxDB方法中以从中检索属性。

  @Bean
  public InfluxDB influxDB(InfluxDBProperties props) {
    InfluxDB influxDB = InfluxDBFactory.connect(props.getUrl(), props.getUsername(), props.getPassword());
    influxDB.query(new Query(CREATE_DB, props.getDbName()));
    influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
    influxDB.setDatabase(props.getDbName());
    return influxDB;
  }

没有更多的静态信息,可以针对每种环境进行配置。

答案 1 :(得分:2)

是的,这应该可行。 Spring可以调用私有构造函数,因此应该不会有任何问题。

但是您为什么要这样做?单例模式与依赖注入的基本租户背道而驰。如果要使用单例InfluxDBAdapter bean,只需使其成为单例bean。

我建议添加一个配置类,看起来像

@Configuration
public class InfluxDBConfig {
    // constants omitted...

    @Bean
    public InfluxDB influxDB() {
        final InfluxDB influxDB = InfluxDB(URL, USERNAME, PWD);
        influxDB.query(new Query(CREATE_DB, DB_NAME));
        influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
        influxDB.setDatabase(DB_NAME);
        return influxDB;
    }
}

然后可以使用InfluxDBAdapter注释@Component,因为可以注入InfluxDB实例。当然,相应地修改InfluxDBInfluxDBAdapter类的构造函数。

其中一些常量可能可以通过配置属性提供,这样您的配置逻辑就不会与业务逻辑混在一起。

相关问题