[英]How to inject static class in Spring-Boot?
這個問題被問了很多次。 我有點困惑,希望你的想法。
我正在 Spring-Boot 中集成 Google Cloud Storage。
我有配置類。
@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();
}
}
我在這樣的service
使用 StorageFactory。
Storage client = StorageFactory.getService()
我讀過Autowired
不會注入靜態成員。 有沒有另一種方法來實現它? 也許這里 Spring-Boot 的功能可以輕松創建單例。
我應該讀什么? 你能給我鏈接嗎? 你能指導我正確的方向嗎?
GCS 示例的鏈接。
我建議不要為此使用靜態方法。 如果您使用 Spring Boot,最好使用配置類來注入單例范圍的Storage
實例:
@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 Boot Environment
注入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;
}
}
第 2 步:創建自己的配置類,其中綁定了配置屬性
@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;
}
}
最后,每當您需要配置類實例時,只需調用CloudConfig.getInstance()
方法並使用該類的 gettter 和 setter 訪問所有必需的數據
get more detail about bean injection
請單擊此處
我希望它能幫助你。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.