I have a small class in my Spring-Boot project in which I am getting the some values from the application.properties file. My class is in src/main/java/com/company/package
folder and my application properties file in src/main/resources/application.properties
. This is my class:
@Component
@Configuration
@PropertySource("classpath:application.properties")
public class ElasticConnection {
private static final String INDEX = "index_name";
public static final String TYPE = "type";
private String user;
private String password;
public ElasticConnection(@Value("${elasticsearch.user}") String user,
@Value("${elasticsearch.password}") String password){
this.user = user;
this.password = password;
}
I am Autowiring this class to the Application class:
@Configuration
@SpringBootApplication
public class ElasticApplication {
@Autowired
ElasticConnection elastic;
public static void main(String[] args) {
SpringApplication.run(ElasticApplication.class, args);
}
}
Now as soon as the constrctor of the ElasticConnection class is called user and password are set to null
. I know the values are read properly because InteliJ is kind enough to show the value (until you click on @Value and shows the "${elasticsearch.user}" again)
EDIT: this is the application properties file.
# App config
server.port=8070
# Local ElasticSearch instance configuration
elasticsearch.clustername = elasticsearch
elasticsearch.host = localhost
elasticsearch.port = 9300
#spring.main.allow-bean-definition-overriding=true
# Elasticsearch Settings
elasticsearch.user=test
elasticsearch.password=test