In this tutorial we are going to see Spring ApplicationContext container with example program.
ApplicationContext
The ApplicationContext container is similar to BeanFactory container.It can load bean definitions, wire beans together and dispense beans upon request.
The ApplicationContext is Interface which is defined in org.springframework.context.ApplicationContext. It adds more enterprise specific functionality such as the ability to resolve textual message from a property file and ability to publish application event to interested event listeners.
ApplicationContext includes all the functionality of BeanFactory.It is complete superset of BeanFactory.The Most commonly used Application context implementations are
The ApplicationContext Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
An ApplicationContext provides:
1. Bean factory methods for accessing application components. Inherited from ListableBeanFactory.
2. The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
3. The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface.
4. The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
5. Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called ApplicationContext.
3. Add the following jars into your build path.
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
4. Now create package com.javatutorialscorner.spring under ApplicationContext Project
5. Next create SpringApplicationContext class under com.javatutorialscorner.spring package
SpringApplicationContext.java
6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variable sayHello without changing the class file.
Now you can run the program see the following output in console.
ApplicationContext
The ApplicationContext container is similar to BeanFactory container.It can load bean definitions, wire beans together and dispense beans upon request.
The ApplicationContext is Interface which is defined in org.springframework.context.ApplicationContext. It adds more enterprise specific functionality such as the ability to resolve textual message from a property file and ability to publish application event to interested event listeners.
ApplicationContext includes all the functionality of BeanFactory.It is complete superset of BeanFactory.The Most commonly used Application context implementations are
- FileSystemXmlApplicationContext
- ClassPathXmlApplicationContext
- WebXmlApplicationContext.
The ApplicationContext Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
An ApplicationContext provides:
1. Bean factory methods for accessing application components. Inherited from ListableBeanFactory.
2. The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
3. The ability to publish events to registered listeners. Inherited from the ApplicationEventPublisher interface.
4. The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
5. Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called ApplicationContext.
3. Add the following jars into your build path.
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
4. Now create package com.javatutorialscorner.spring under ApplicationContext Project
5. Next create SpringApplicationContext class under com.javatutorialscorner.spring package
SpringApplicationContext.java
package com.javatutorialscorner.spring;
public class SpringApplicationContext {
private String sayHello;
public void getSayHello() {
System.out.println("Java Tutorials Corner " + sayHello);
}
public void setSayHello(String sayHello) {
this.sayHello = sayHello;
}
}
6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
package com.javatutorialscorner.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RunApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SpringApplicationContext applicationContext = (SpringApplicationContext)context.getBean("applicationContext");
applicationContext.getSayHello();
}
}
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.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.xsd">
<bean id="applicationContext" class="com.javatutorialscorner.spring.SpringApplicationContext">
<property name="sayHello" value="say Hello"></property>
</bean>
</beans>
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variable sayHello without changing the class file.
Now you can run the program see the following output in console.
Java Tutorials Corner say Hello
0 comments:
Post a Comment