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
click on XML foldernow 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:
No comments:
Post a Comment