2017-09-19 1 views
2

Ich habe folgendes:Bean Instanziierung Bestellen

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

    @Inject 
    private CustomConfig customConfig; 


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

Wer weiß, wie ich die Werte des customConfig.getDropboxAppToken(); bekommen zuerst zu laden. Ich erhalte die folgende Fehlermeldung: Fehler Bean mit dem Namen 'ecosmart_BackupService' zu schaffen:

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 

Aktuelle Code I

ERROR versuchen Unbefriedigend Abhängigkeit ausgedrückt durch Feld 'dropboxService'; Verschachtelte Ausnahme ist org.springframework.beans.factory.BeanCreationException: Fehler beim Erstellen einer Bean mit dem Namen 'ecosmart_DropboxService': Aufruf der init-Methode fehlgeschlagen; verschachtelte Ausnahme ist 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

dass Legen Sie in einer '@ PostConstruct' kommentierten Methode. Die Injektion kann nur stattfinden, nachdem ein Objekt erstellt wurde. –

+0

Ich habe mit dem Versuch @ Postconstruct aktualisiert. Es funktioniert jedoch immer noch nicht. Vermutlich, weil die Bean instanziiert wird, bevor die Bean CustomConfig beim Hochfahren gestartet wird ... – Daryn

+0

Sie haben mit 'myApp_DropboxService' begonnen und jetzt haben Sie ein Problem mit' ecosmart_BackupService', so dass das Problem mit dem Code in dieser Klasse wahrscheinlich ähnlich ist . – Oleg

Antwort

1

Frühling spritzt Felder nur, nachdem das Objekt gebaut wurde und in Ihrem Fall ACCESS_TOKEN initialisiert sogar davor.

Sie benötigen einen Konstruktor erstellen und Ihren Bean im Konstruktor injizieren etwa so:

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

Getting this: Bean Instanziierung über Konstruktor fehlgeschlagen; verschachtelte Ausnahme ist org.springframework.beans.BeanInstantiationException: Fehler beim Instanziieren von [com.daryn.ecosmart.service.DropboxServiceBean]: Konstruktor hat Ausnahme ausgelöst; verschachtelte Ausnahme ist java.lang.NullPointerException: accessToken – Daryn

+0

@Daryn Aktualisieren Sie Ihre Frage mit Ihrem aktuellen Code. – Oleg

+0

@Daryn Sie müssen 'neues DbxClientV2 (config, ACCESS_TOKEN);' innerhalb des Konstruktors verschieben. – Oleg

-1

Nun, nach vielen herumschlagen, hier ist meine große Lösung. Ich habe keine Ahnung, warum die CustomConfig nicht zuerst intialise ...

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