스프링 부트 프로젝트에 여러 클래스가 있습니다. 일부는 @Autowired와 함께 작동하지만 일부는 그렇지 않습니다. 여기 내 코드는 다음과 같습니다스프링 부트 Autowired null
Application.java (@Autowired 작품) :
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 작품) :
package com.example.myproject.controller;
@Controller
class HomeController {
@Resource
MyRepository myRepository;
이면 MyService을 (@Autowired가 작동하지 않습니다) :
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
SOAPConfiguration이 표시되지 않지만 시도 할 때 내 응용 프로그램이 null 포인터 예외로 중단됩니다. 그것을 액세스하십시오.
나는 많은 스레드를 이미 읽고 봤지만 아직 해결 방법을 찾지 못했습니다. 필요한 정보를 모두 제공해 주셨습니다. 빠진 것이 있으면 알려주세요.
여기서 'init' 메서드는 어디에서 호출합니까? 나는 생성자를 의심한다. –
log.info prints : starting init, soapConfiguration : null – dexBerlin
HomeController.update는 새로운 MyServiceImpl을 만들고 init 메소드를 호출하는 myService.update를 호출합니다. – dexBerlin