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:


Tuesday 26 October 2010

BeanFactory

What is BeanFactory?

BeanFactory is an interface.
Now we have seen the overview of ApplicationContext , and one more interface is called BeanFactory provided by Spring people for the same purpose .

ApplicationContext does everything which BeanFactory does a some more tasks.  In the current Spring release Spring is clearly mentioned that it has been provided BeanFactory just for the backward compatibility .
So unless and until you have a very strong reason to use BeanFactory , we should rely on ApplicationContext to use spring framework.


You may like the following posts:

Injecting Value into a BeanFactory

Sunday 17 October 2010

First Spring Example program 2



Steps 2: create out First Spring Application:

1.   Create Java Class (Ex: Restaurant class)
         This is a simple java class which just print a “welcome” message to the console
2.                   Create Spring Configuration File-(SpringConfig.xml)
It is an XML file Spring framework will read this xml file for managing all java objects  in our projet
3.                   Write a test class
This is for testing the application

Step1:
Create the Restaurant class:

Go to the src folder from the project
Choose new option
Click on class






Let's write Spring Configuration file
Go to src folder
Right click


Choose "other"
Now we will get like below:

Go to XML 
click on XML folder
now you will get like below:
Select XML file

Click on "next" button

Give the file name (Here I'm giving SpringConfig.xml)

You can give any name instead of SpringConfig.xml
click next button
click on next
click on finish


Write the xml file:
SpringConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="restaurantBean" class="org.rajendra.springcore.Restaurant">
</bean>
</beans>

In Spring Framework every object is called bean. So  don't think bean is a new to us.

So now onwards bean means Java object only. Here restaurantBean is a object. Instead of restaurantBean we can give any name.

Now I want to write Test class. Create Test class in same project.

So you know all the steps to write the Creating another class.

// TestSpringProject
package org.rajendra.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringProject {
 public static void main(String args[])
 {
     ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml");
     Restaurant restaurant=(Restaurant)context.getBean("restaurantBean");
     restaurant.greetCustomer();
 }
}

Explanation about this code:

     ApplicationContext context=new ClassPathXmlApplicationContext("SpringConfig.xml");

This statement is going to find load "SpringConfig.xml " file from our project into the application context.
What is ApplicationContext?
It is simply provided by spring people which reads simply reads the configuration of your project from XML file and then by calling different methods by calling its different functionalities provided by spring people like Dependency Injection concept , Aspect oriented programming etc,..

In simple words without any complexity this is the basic interface provided to you to access Spring Framework.

Restaurant restaurant=(Restaurant)context.getBean("restaurantBean");?
Here we are calling getBean() method of ApplicationContext interface. Just request spring framework to create and return an object (bean) with a bean name restaurantBean.
Here Spring framework will search for this bean in the configuration file and implicitly create and return an object for the class mentioned with the bean name: restaurantBean.

restaurant.greetCustomer();?
And here we are simply calling the greetCustomer() of a restaurant bean or restaurant object to print a welcome note on console.

Now everything is set our application is ready.

Now let us run this application:

right click->Run As->Java Application
We are getting errors ?
Because SpringConfig.xml file should be out side of package, so move the SpringConfig.xml file out side of package. Copy the configuration file and paste into "src" folder.





Now we will see the overview :

What is BeanFactory?

BeanFactory is an interface.
Now we have seen the overview of ApplicationContext , and one more interface is called BeanFactory provided by Spring people for the same purpose .

ApplicationContext does everything which BeanFactory does a some more tasks.  In the current Spring release Spring is clearly mentioned that it has been provided BeanFactory just for the backward compatibility .
So unless and until you have a very strong reason to use BeanFactory , we should rely on ApplicationContext to use spring framework.


Next:

Injecting Value into a BeanFactory

You may like the following article: