I have a spring boot application and 'mvn test' is failing because Autowired is not working for reading value from application.yml file.
Class file:
@Configuration
public class LogConfig {
private SettingsRepository settingsRepository;
@Value("${build.version}")
private String buildVersion;
@Value("${build.name}")
private String buildName;
public LogConfig(SettingsRepository settingsRepository) {
this.settingsRepository = settingsRepository;
}
@Scheduled(fixedDelay = 5000)
public void injectDataForLogging() throws SocketException {
MDC.put("applicationToken", settingsRepository.getToken().isPresent() ? settingsRepository.getToken().get() : "NO-TOKEN");
MDC.put("macAddressList", MACAddressUtil.getMACAddresses().toString());
MDC.put("application", buildName);
MDC.put("applicationVersion", buildVersion);
}
}
application.yml:
build:
version: @project.version@
name: @project.artifactId@
pom.xml:
<artifactId>something</artifactId>
<version>2.0.0</version>
Error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'build.version' in value "${build.version}"
When I run the application, everything works normally :/