Java 如何在Spring Boot中注入静态类?

Java 如何在Spring Boot中注入静态类?,java,spring,spring-boot,google-cloud-storage,Java,Spring,Spring Boot,Google Cloud Storage,这个问题被问了很多次。我有点困惑,希望你的想法 我正在SpringBoot中集成Google云存储 我有配置类 @Component @ConfigurationProperties(prefix = "gcs.credentials") public class GCSConfig { private String serviceAccountId; //... } 实现单例模式的存储工厂 @Component public class StorageFactory {

这个问题被问了很多次。我有点困惑,希望你的想法

我正在SpringBoot中集成Google云存储

我有配置类

@Component
@ConfigurationProperties(prefix = "gcs.credentials")
public class  GCSConfig {

    private String serviceAccountId;

    //...
}
实现单例模式的存储工厂

@Component
public class StorageFactory {

    @Autowired
    private static GCSConfig gcsConfig;

    private static Storage instance = null;

    public static synchronized Storage getService() throws GeneralSecurityException, IOException {
        if (instance == null) {
            instance = buildService();
        }
        return instance;
    }

    private static Storage buildService() throws GeneralSecurityException, IOException {

        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        // ...
        // I use gcsConfig here
        // ...
        return new Storage.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("Google Cloud Storage App")
        .build();
    }
}
我在这样的服务中使用StorageFactory

Storage client = StorageFactory.getService()
我读过Autowired不会注入静态成员。有没有其他方法来实现它?也许这里有Spring Boot的特性,可以轻松创建单例

我应该读什么?你能给我链接吗?你能指引我正确的方向吗


我建议不要为此使用静态方法。如果您使用的是Spring Boot,则最好使用注入单例作用域存储实例:

@Configuration
public class StorageServiceConfiguration {

  private GCSConfig gcsConfig;

  // I prefer using explicit setters for testing, YMMV
  @Autowired
  public void setGcsConfig(GCSConfig gcsConfig) {
    this.gcsConfig = gcsConfig;
  }

  @Bean
  public Storage getStorageService() {
     // Logic to create your Storage instance.
     return new Storage.Builder(...) ...;
  }
}

根据您对GCSConfig的操作,您可以将属性值或Spring引导环境注入StorageServiceConfiguration类,并跳过中间对象

不需要注入静态类。注入意味着已经创建了一个实例。您的引用类只需使用import static…

您可以按照以下方式执行此操作,它最适合您的用例

步骤1:创建能够返回bean类实例的新类

package com.xyx;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    private ApplicationContextProvider(){}

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public  static <T> T getBean(String name,Class<T> aClass){
        return context.getBean(name,aClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        context = ctx;
    }
}
最后,无论何时需要配置类实例,只要调用 方法,并使用该类的getter和setter访问所有必需的数据

获取更多关于豆子注射的详细信息


我希望它能帮助你。谢谢

您知道Springbeans的默认作用域是singleton吗?因此,在专门用作Springbean的类中实现singleton模式没有多大意义。
@Component
@ConfigurationProperties(prefix = "gcs.credentials")
public class  CloudConfig {
    private static CloudConfig objectInstance=null;

    private String serviceAccountId;

    public String getServiceAccountId() {
        return serviceAccountId;
    }

    public void setServiceAccountId(String serviceAccountId) {
        this.serviceAccountId = serviceAccountId;
    }

    public static CloudConfig getInstance(){
        if(objectInstance==null){
            objectInstance=ApplicationContextProvider.getBean("cloudConfig",CloudConfig.class);
        }
        return objectInstance;
    }
}