Wednesday 27 October 2010

Injecting Value into a BeanFactory


In the first program we have created a Restaurent class which greets a method which reads a customer when we call greetCustomer() method then we put it entry in SpringConfig.xml file and provided its name as restaurentBean and then we wrote TestSpringProject class where we requested to spring framework to get a Restaurent object.
And then we call greetCustomer() method which displayed "Welcome to Our Restaurent" on the console.


In this article we can show to set up property which is in the Restaurent class from the configuration file SpringConfig.xml
We can write spring property in the Restaurent class and then provide its value from configuration file SpringConfig.xml .


Step 1: Go to the Restaurent class:
Step 2: Include a property:
Step 3: write setter method
Step 4: Im removing hardcoded welcome message,
 and use welcomenote property to display "Welcome note" on the console
package org.rajendra.springcore;
public class Restaurant
 {
    String welcomenote;
    public void setWelcomeNote(String welcomenote)
    {
        this.welcomeNote=welcomeNote;
    }
    public void greetCustomer()
    {
//        System.out.println("Welcome to Restaurant");
       System.out.println(welcomenote);
    }
}

Now what we want is ? we want to provide its property' s value (welcomeNote) from this configuration file,
Step 5:

So we have to modify spring configuration file as well.



















You may like the following posts:


No comments:

Post a Comment