2016-07-20 3 views
-2

この質問は何度も聞かれました。私は少し混乱して、あなたのアイデアを願っています。静的クラスSpring-Bootを挿入するには?

私はSpring-BootでGoogle Cloud Storageを統合しています。

私はconfigクラスを持っています。

@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を使用します。

私は、Autowiredは静的メンバーを挿入していないと読んでいます。それを実装する別の方法がありますか?たぶんシングルトンを簡単に作成するSpring-Bootの機能です。

どうすればよいですか?リンクを教えてください。私を正しい方向に導くことができますか?

GCS examplesのリンク。

+1

春の豆のデフォルトスコープは "シングルトン"ですか?そのため、Spring Beanとして排他的に使用されるクラスでは、シングルトンパターンを実装するのは大変意味がありません。 – rmlan

答えて

3

この目的で静的メソッドを使用しないことをお勧めします。

@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(...) ...; 
    } 
} 

あなただけのプロパティ値や春ブーツEnvironmentを注入でき、何をGCSConfigでやっているに応じて:あなたは春のブートを使用している場合には、シングルトンがStorageインスタンスのスコープ注入するconfiguration classを使用する方が良いだろうStorageServiceConfigurationクラスに追加し、中間オブジェクトをスキップします。

1

静的クラスを挿入する必要はありません。インジェクションとは、インスタンスが作成されたことを意味します。あなたの参照クラスは、ちょうどあなたは、次の方法として、この操作を行うことができimport static ...

1

を使用しなければならないことでしあなたのユースケースでベストフィット

ステップ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:いつでもY今あなたの設定のプロパティが最後に

@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; 
    } 
} 

バインドされている独自の設定クラスを作成しますOUは、コンフィギュレーションクラスのインスタンスは、ちょうど CloudConfig.getInstance()メソッドを呼び出すと、私はそれはあなたを助けることを願っています、そのクラス

get more detail about bean injectionclick here

のgettterとセッターを使用して、必要なすべてのデータにアクセスする必要があります。ありがとう。

関連する問題