Muffinous
Muffinous

Reputation: 33

load properties file and access later from another class - spring

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);
  }

Upvotes: 0

Views: 83

Answers (1)

Samuel Marchant
Samuel Marchant

Reputation: 320

The following in the code is a ResourceBundle obtained as a PropertyResourceBundle , however, i think you are simply not calling across the classes to obtain the reference. property .properties files use a line each a key and value-property separated by an equal sign. Of essence, when you obtain your "ResourceBundle" object , it should been placed on a global reference in the class and a method to pass it from the class and the other class with a reference to the class that has the property reference. It's also a wonder you do not have error messages from "main" of "non static method cannot be used in a static context". Much of what you are doing in classB you should be doing in classB constructor with a global reference in it to class Config.

java.io.FileInputStream strprop;
java.util.PropertyResourceBundle javPropbun;
java.io.File propfile;
java.util.Enumeration<String> propKeys;
// -Xss=1024k
// -Xms=1024m
// -Xmx=2048m
try{
propfile = new java.io.File("someproperties.properties");

if(propfile.exists()){
if(propfile.canRead()){
strprop = new java.io.FileInputStream(propfile);
javPropbun = new java.util.PropertyResourceBundle((java.io.InputStream)strprop);
propKeys = (java.util.Enumeration<String>)javPropbun.getKeys();
String kout = "";
while(propKeys.hasMoreElements()){
kout = (String)propKeys.nextElement();
System.out.println("Inserted to STDOUT logging - "+kout+"="+javPropbun.handleGetObject(kout);
}//enwhl

}else{
System.out.println("ERR: someproperties.properties cannot be read");
}
}

}catch (java.io.FileNotFoundException efx){
efx.printStackTrace();
}catch (java.io.IOException iox){
iox.printStackTrace();
}




public class classB {

Config config;

public classB(){
config = new Config();
// ask for info from Config ....
}

    public static void main(String[] args) {
new classB();
   }

}//enclss

Upvotes: 1

Related Questions