0

I have spring boot application where the user sends parameter and based on this parameter, there are configuration should be get.

I created file name configuration type “Configration_Types.properies” , now how can I read the configuration based on parameter pass I don't want to create database to lookup it?

Type1=Type1
Type1.width=60
Type1.heght=715

Type2=Type2
Type2.width=100
Type2.heght=720

Type3=Type3
Type3.width=100
Type3.heght=700

Type4=Type4
Type4.width=450
Type4.heght=680

Type5=Type5
Type5.width=270
Type5.heght=750

for example pass type4 should get configuration

Type4

450

680

Jdonut
  • 31
  • 6
user1122
  • 43
  • 1
  • 1
  • 7
  • Have you tried anything? Please share your code. I'm sure there is a description to be found here: http://spring.io/ – leonardkraemer Apr 03 '18 at 08:56
  • how are you trying to access properties file? With XML or through java class? – Adya Apr 03 '18 at 08:58
  • I hardly understand what you are really asking for, but have you checked: [access-properties-programmatically](https://stackoverflow.com/questions/1771166/access-properties-file-programmatically-with-spring) or [read-values-from-properties](https://stackoverflow.com/questions/9259819/how-to-read-values-from-properties-file) if I understood the question correctly, I would consider it as duplicate... – Haphil Apr 03 '18 at 08:58
  • @Adya java classes – user1122 Apr 03 '18 at 09:04

2 Answers2

0

The function could be like that :

public void readPropertyByType(String type)
    {
        InputStream input = null;
        try
        {
            Properties prop = new Properties();

            // if your type is Type4, the typeKey will be Type4. for compare data
            String typeKey = type + ".";

            String filename = "config.properties";
            input = new FileInputStream(filename);
            prop.load(input);

            String typeInformation = "";
            Enumeration< ? > e = prop.propertyNames();
            while (e.hasMoreElements())
            {
                String key = (String)e.nextElement();
                if (key.indexOf(typeKey) > 0)
                {
                    typeInformation = typeInformation + prop.getProperty(key);
                }
            }
            System.out.println("The data of type " + type + "is :" + typeInformation);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if (input != null)
            {
                try
                {
                    input.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
  • But you make a better design for your property file.
  • In case the order of property file never change, you can stop the function when you get all expected data, don't need to loop whole file.

Hope it help.

---UPDATE---

The property file could be like that :

  • Option 1

Type4.width=450

Type4.heght=680

Type5.width=450

Type5.heght=680

  • Option 2

Type4=450, 680

Type5=450, 680

Depend on each option, you can break the while loop when you got the expected data.

BIZ
  • 113
  • 6
0

If you can modified your property file Configration_Types.properies like:-

type[0].width=60
type[0].heght=715

type[1].width=100
type[1].heght=720

type[2].width=100
type[2].heght=700


type[3].width=450
type[3].heght=680

type[4].width=270
type[4].heght=750

And your class for consume property values from property file would be:-

@Component
@ConfigurationProperties("type") // prefix type, find type.* values
public class GlobalProperties {

    private List<Type> type = new ArrayList<Type>();

    //getters and setters

  public static class Type
  {
      private int width;
      private int heght;
     // getter setter

}

And As based on user params you can access the value from arraylist.

hope this help:

gajju_15
  • 527
  • 4
  • 17
  • there are another option rather than indexing in **properties file ** i prefer by name it's will be less confuse – user1122 Apr 03 '18 at 12:06
  • yes, that is also possible , but i dont think there is much diffrence with the approach you are trying based on user input as( 1,2 etc). you can use LinkedList for maintain the order and get the propery by indexing. – gajju_15 Apr 03 '18 at 13:06