I'm loading a properties file in java in the following way in class A (loadConfig is called as soon as the program is executed and it is executed without any problem, what's more, the register of showing configProp.getAttribute("home") returns me the correct value of the configuration file:
public class config {
private static Properties properties; // this file contains the name of the wfile.properties and route that I want to 'save' in configProp to access later
@Autowired
public final Properties configProp = new Properties();
public Properties getProperties() {
return configProp;
}
public void loadconfig() {
File wFile = new File(properties.getProperty("wfile.properties");
try (FileInputStream fis = new FileInputStream(wFile)) {
configProp.load(fis); // this is loading without any problem
getLogger(HASH).info("***FILE LOADED configProp*** ==== " + configProp); // PRINTS THE CORRECT FILE AND ALL OF THE VALUES
getLogger(HASH).info("***FILE LOADED configProp*** ==== " + configProp.getAttribute("home"); // PRINTS THE CORRECT VALUE FROM THE PROPERTIES FILE
fis.close();
} catch (IOException e) {
getLogger(HASH).error("Error loading configProp properties file", e);
throw new RuntimeException(e);
}
}
The problem is, that I need to access these attributes from another class later, I am doing it in the following way:
public class classB {
public static void main(String[] args) {
Config config = new Config();
getLogger(HASH).info(config.getProperties().getProperty("home")); // RETURNS NULL I DONT KNOW WHY
String home1 = config.getProperties().getProperty("home"); // RETURNS NULL I DONT KNOW WHY
}
I'm using spring and that's why I put @autowired in the variable, but it still returns null. I hope I have explained it clearly, I have tried several things like putting the load in the constructor but it made a loop.
Thank you so much.
Edit: I also tried loading the file like:
final ClassLoader loader = getClass().getClassLoader();
try(InputStream config = loader.getResourceAsStream(properties.getProperty(wfile.properties))){
configProp.load(config);
} catch(IOException e){
throw new RuntimeException(e);
}