2014-10-20 23 views
6

Ich habe mehrere Klassen in einem Spring Boot-Projekt, einige arbeiten mit @Autowired, andere nicht. Hier ist mein Code folgt:Spring Boot Autowired null

Application.java (@Autowired Werke):

package com.example.myproject; 

@ComponentScan(basePackages = {"com.example.myproject"}) 
@Configuration 
@EnableAutoConfiguration 
@EnableJpaRepositories(basePackages = "com.example.myproject.repository") 
@PropertySource({"classpath:db.properties", "classpath:soap.properties"}) 
public class Application { 

@Autowired 
private Environment environment; 

public static void main(String[] args) { 
    SpringApplication.run(Application.class); 
} 

@Bean 
public SOAPConfiguration soapConfiguration() { 
    SOAPConfiguration SOAPConfiguration = new SOAPConfiguration(); 
    SOAPConfiguration.setUsername(environment.getProperty("SOAP.username")); 
    SOAPConfiguration.setPassword(environment.getProperty("SOAP.password")); 
    SOAPConfiguration.setUrl(environment.getProperty("SOAP.root")); 
    return SOAPConfiguration; 
} 

Homecontroller (@Autowired Werke):

package com.example.myproject.controller; 

@Controller 
class HomeController { 

    @Resource 
    MyRepository myRepository; 

MyService (@Autowired funktioniert nicht):

Ich bekomme nicht die SOAPConfiguration, aber meine Anwendung bricht mit einer Nullzeigerausnahme, wenn ich es versuche um darauf zuzugreifen.

Ich habe schon viele Threads hier gelesen und gegoogelt, aber noch keine Lösung gefunden. Ich habe versucht, alle notwendigen Informationen zu liefern, bitte lassen Sie mich wissen, wenn etwas fehlt.

+0

Woher rufen Sie die 'init'-Methode? Ich vermute den Konstruktor. –

+0

log.info druckt: start init, soapConfiguration: null – dexBerlin

+0

HomeController.update erstellt ein neues MyServiceImpl und ruft myService.update auf, das seine init-Methode aufruft. – dexBerlin

Antwort

8

Ich nehme an, Sie rufen init() bevor das Autowiren stattfindet. Annotate init() mit @PostConstruct, um es nach all dem Frühjahr Autowiring automatisch aufzurufen.

EDIT: nachdem ich Ihren Kommentar gesehen habe, denke ich, dass Sie es mit new MyServiceImpl() erstellen. Dies nimmt die Kontrolle des MyServiceImpl von Spring und gibt es Ihnen. Autowiderstellung funktioniert in diesen Fällen nicht

+0

Ich habe die Annotation @PostContruct hinzugefügt, aber soapConfiguration ist immer noch null. – dexBerlin

+0

siehe die bearbeitete Antwort – sinu

+0

Vielen Dank, das hat mein Problem gelöst. – dexBerlin

1

Haben Sie eine Bean für die Klasse SOAPConfiguration in einer Ihrer Konfigurationsklassen erstellt? Wenn Sie eine Klasse in Ihrem Projekt automatisch ansteuern möchten, müssen Sie eine Bean dafür erstellen. Beispiel:

@Configuration 
public class SomeConfiguration{ 

    @Bean 
    public SOAPConfiguration createSOAPConfiguration(){ 

     return new SOAPConfiguration(); 
    } 

} 

public class SomeOtherClass{ 

    @Autowired 
    private SOAPConfiguration soapConfiguration; 
} 
+0

Die Application.java sollte das tun? Wie in meiner Erklärung? – dexBerlin

+0

Sind Sie sicher, dass die Methode in Ihrer Application.java SOAPConfiguration aufgerufen hat? – furkan3ayraktar

Verwandte Themen