Mam kilka klas w projekcie Spring Boot, niektóre pracuję z @Autowired, inne nie. Oto mój kod następująco:Spring Boot Autowired null
Application.java (@Autowired robót):
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 działa):
package com.example.myproject.controller;
@Controller
class HomeController {
@Resource
MyRepository myRepository;
MyService (@Autowired nie działa):
package com.example.myproject.service;
@Service
public class MyServiceImpl implements MyService {
@Autowired
public SOAPConfiguration soapConfiguration; // is null
private void init() {
log = LogFactory.getLog(MyServiceImpl.class);
log.info("starting init, soapConfiguration: " + soapConfiguration);
url = soapConfiguration.getUrl(); // booom -> NullPointerException
Nie otrzymuję konfiguracji SOAP, ale moja aplikacja zrywa z wyjątkiem wskaźnika pustego, gdy próbuję aby uzyskać do niego dostęp.
Przeczytałem już wiele wątków i przeglądałem, ale nie znalazłem jeszcze rozwiązania. Próbowałem dostarczyć wszystkie niezbędne informacje, proszę dać mi znać, jeśli coś nie trafi.
Skąd nazwałaś metodę "init"? Podejrzewam konstruktora. –
drukuje log.info: start init, soapConfiguration: null – dexBerlin
HomeController.update tworzy nowy MyServiceImpl i wywołuje myService.update, który wywołuje jego metodę init. – dexBerlin