2017-09-19 6 views
2

私は、次があります。エラー名でBeanを作成する 'ecosmart_BackupService':私はビーンインスタンスは、注文

ERRORしようとしています

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myApp_DropboxService' defined in URL 
[jar:file:/E:/Cuba/myApp/deploy/tomcat/webapps/app-core/WEB-INF/lib/app-core-0.1-SNAPSHOT.jar!/com/daryn/myApp/service/DropboxServiceBean.class]: 
Instantiation of bean failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [com.daryn.myapp.service.DropboxServiceBean]: Constructor 
threw exception; nested exception is java.lang.NullPointerException 

現在のコード:私は、次のエラーを取得しておく不満依存関係がフィールドを通して表現 'をdropboxService';ネストされた例外はorg.springframework.beans.factory.BeanCreationExceptionです: 'ecosmart_DropboxService'という名前のBeanを作成中にエラーが発生しました:initメソッドの呼び出しに失敗しました。ネストされた例外は、春には、オブジェクトが構築された後にのみフィールドを注入し、ACCESS_TOKENでもその前に初期化されて、あなたのケースでjava.lang.NullPointerExceptionが

@Service(DropboxService.NAME) 
public class DropboxServiceBean implements DropboxService { 

    @Inject 
    private CustomConfig customConfig; 


    private String ACCESS_TOKEN = ""; 
    DbxRequestConfig config; 
    DbxClientV2 client; 



    @PostConstruct 
    public void postConstruct() { 
     System.out.println("**************Running post construct"); 
     ACCESS_TOKEN = customConfig.getDropboxAppToken(); 
     config = new DbxRequestConfig("dropbox/java-tutorial", "en_US"); 
     client = new DbxClientV2(config, ACCESS_TOKEN); 
    } 
+0

これを '@ PostConstruct'アノテーション付きメソッドに入れます。注入は、オブジェクトが作成された後にのみ行うことができます。 –

+0

@postconstructを試して更新しました。しかし、それはまだ動作していません。おそらく、Beanが起動時にCustomConfig Beanを起動する前にBeanがインスタンス化されているためです... – Daryn

+0

あなたは 'myApp_DropboxService'で始めましたが、今は' ecosmart_BackupService'に問題がありますので、問題はこのクラスのコードとおそらく同じです。 – Oleg

答えて

1

です。あなたはコンストラクタを作成して、そのようなコンストラクタであなたのBeanを注入する必要がある

@Inject 
public DropboxServiceBean(CustomConfig customConfig) { 
    this.customConfig = customConfig; 
    ACCESS_TOKEN = customConfig.getDropboxAppToken(); 
} 
+0

入手方法: コンストラクタを使用したBeanのインスタンス生成に失敗しました。ネストされた例外はorg.springframework.beans.BeanInstantiationExceptionです:[com.daryn.ecosmart.service.DropboxServiceBean]のインスタンス化に失敗しました:コンストラクターが例外をスローしました。ネストされた例外はjava.lang.NullPointerExceptionです:accessToken – Daryn

+0

@Darynあなたの質問をあなたの現在のコードで更新してください。 – Oleg

+0

@Darynコンストラクタ内で 'new DbxClientV2(config、ACCESS_TOKEN);'を移動する必要があります。 – Oleg

-1

まあ、あまり周りいじくる後、ここに私の偉大なソリューションです。私はなぜCustomConfigが最初に初期化しないのか分かりません。

@Service(DropboxService.NAME) 
public class DropboxServiceBean implements DropboxService {   


    @Inject 
    private CustomConfig customConfig; 


    private String ACCESS_TOKEN = ""; 
    DbxRequestConfig config =new DbxRequestConfig("dropbox/java-tutorial", "en_US"); 
    DbxClientV2 client; 

    public static boolean isInitiated = false; 

    public void generateDbxClient(){ 
     ACCESS_TOKEN = customConfig.getDropboxAppToken(); 
     client = new DbxClientV2(config, ACCESS_TOKEN); 
    } 

    @Override 
    @Transactional 
    public void uploadFile(FileDescriptorExt file, String path) { 

     if(isInitiated==false){ 
      System.out.println("generating client"); 
      generateDbxClient(); 
      isInitiated=true; 
     }