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.
Woher rufen Sie die 'init'-Methode? Ich vermute den Konstruktor. –
log.info druckt: start init, soapConfiguration: null – dexBerlin
HomeController.update erstellt ein neues MyServiceImpl und ruft myService.update auf, das seine init-Methode aufruft. – dexBerlin