0

I am stuck in an operation. I have 2 POJO Bean Classes

class A{
    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
}

..................................................................

class B {
    private String company;
    private Object object;

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getObject() {
        return object;
    }

    public void setObject(String object) {
        this.object = object;
    }

}

..........................................................

class SampleTest {
    public static void main(String[] args) {
        A a = new A();
        a.setName("Some Data");

        B b = new B();
        b.setCompany("Stack Overflow");
        b.setObject(a);

        //...... Next Lines.....
    }
}

Is there any way to set data in B pojo class by reading any Property file?

Example Property file:

#Property File<br>
B.company = Stack Overflow<br>
B.object.name = Some Data

Please help. Thank you

MariuszS
  • 30,646
  • 12
  • 114
  • 155
rDX
  • 228
  • 1
  • 4
  • 15
  • 1
    How is your question related to Spring and reflection? Is see not Spring context. –  Nov 22 '13 at 14:53
  • 1
    refer http://stackoverflow.com/questions/8285595/reading-properties-file-in-java – codingenious Nov 22 '13 at 14:55
  • My Problem is not related to "How to read a property file". he issue is how to set data in bean by reading a fields name and its value from property file. I am sorry if my question confuse any one. – rDX Nov 22 '13 at 15:03
  • 1
    you have public setters for POJO, call that will value you want to set, where is the proble?? – codingenious Nov 22 '13 at 15:07
  • `public String getObject()`, but your `object` is `Object.class`? – MariuszS Nov 22 '13 at 15:09

1 Answers1

0

One way can be :

Properties prop = new Properties();
try
{
    // load a properties file
    prop.load(new FileReader("config.properties"));

    // get the property value
    B b = new B();
    b.setCompany(prop.getProperty("B.company"));

}
catch (IOException ex)
{
    ex.printStackTrace();
}
codingenious
  • 8,385
  • 12
  • 60
  • 90
  • Hi, Thank you for you reply. But this is not the issue. my problem is How to set data in POJO beans by simply reading fields and its values of POJOs from property file. – rDX Nov 22 '13 at 15:05
  • I think it is more related to Reflections – rDX Nov 22 '13 at 15:06