Nearby lessons
4 of 35Spring - ApplicationContext
- Understand Spring - ApplicationContext
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring - ApplicationContext step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
ApplicationContext
ApplicationContext IOC Container is an extension of BeanFactory IOC Container , it able
to provide some advanced features like Internationalization, Event Handling,..... along
with fundamental functionalities what BeanFactory is providing.
In Spring, ApplicationContext IOC Container is represented in the form of the following
predefined interface.
"org.springframework.context.ApplicationContext".
SpringFramework has provided ApplicationContext as a chaild interface to BeanFactory
interface.
SpringFramework has provided the following three implementation classes for
ApplicationContext.
1.ClassPathXmlApplicationContext:
--> It able to get all the beans configuration details from confguration file which is existed
in application classpath.
2.FileSystemXmlApplicationContext:
--> It able to get all the beans configuration details from Configuration file which is
existed at our system harddisk.
3.WebXmlApplicationContext:
--> It able to get all the beans configuration details from configuration file which is
existed in web application.
Example:
HelloBean.java
--------------
package com.durgasoft.beans;
public class HelloBean {
static {
System.out.println("Bean Loading.....");
}
public HelloBean() {
System.out.println("Bean Created....");
}
public String sayHello() {
return "Hello User";
}
}
applicationContext.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="helloBean" class="com.durgasoft.beans.HelloBean"/>
</beans>
Test.java
package com.durgasoft.test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.durgasoft.beans.HelloBean;
public class Test {
public static void main(String[] args) throws Exception{
ApplicationContext context = new
ClasspathXmlApplicationContext("applicationContext.xml");
HelloBean bean = (HelloBean)context.getBean("helloBean");
System.out.println(bean.sayHello());
}
}
In the above application, when we activate BeanFactory container then BeanFactory
Container will perform the following actions.
- It will take bean configuration file name and location from Container class constructor.
- It will search for the respective bean configuration file at the specified location.
- If the respective bean configuration file is available then ApplicationCntext container
will load that xml file to the memory.
- After XML file loading, ApplicationContext Container will parse that xml file, that is, it
will check all the tags in XML file are provided properly or not, all attributes are available
properly or not.
- After the XML file parsing, ApplicationContext Container will read data from Beans
configuration file.
- If any bean configuration is identified in beans configuration file the
ApplicationContext container will take beans classes and their locations.
- ApplicationContext Container will search for the respective beans at the specified
locations, if they are available then IOC Container will load all the bean classes bytecode
to the memory.
- ApplicationContext Container will create Objects for the loaded bean classes and their
dependent bean objects.
- ApplicationContext Container will store all the bean objects and their dependent
objects in Container object in the form of Key-Value pairs, where keys must be the "id"
attribute values which we specified in beans configuration file.
In the above context, if we access getBean("--") method over Container reference then
ApplicationContext Container will search for the Bean object on the basis of the provided
bean identity , if it is available then AppliationContext Container will return Bean object.
Q) What are the differences between BeanFactory and ApplicationContext IOC
Containers?
Answer:
ApplicationContext
functionalities to the spring applications like creating and maintaning bean objects.
ApplicationContext IOC Container is an extension of BeanFactory IOC Container , it able
to provide some advanced features like Internationalization, Event Handling,..... along
with fundamental functionalities what BeanFactory is providing.
ApplicationContext
ApplicationContext is supporting to integrate AOP services like Security, JTA,... to the
spring applications.
ApplicationContext
basis of Spring web module.
ApplicationContext is suitable for the web applications which we want to prepare on the
basis of Spring web module.
ApplicationContext
that is, Lazy Instantiation/Initialization.
ApplicationContext is able to prepare Singleton objects when we activate Container , that
is , early Instantiation/Initialization.
5.BeanFactory is supporting only the scopes like Singleton and Prototype.
ApplicationContext is supporting almost all the Spring scopes like Singleton, Prototype,
request, session, globalSession, webSocket,...
6.BeanFactory is mainly for Standalone Applications.
ApplicationContext is for all the types of Spring famework applications.
7.BeanFactory is an outdated Container in Spring applications.
ApplicationContext is not outdated Container.
Beans In Spring Framework
Bean Definition: Bean is a Software Reusable Component, it is a normal java class
contains properties and the corresponding setXXX(-) and getXXX() methods and which
are created and managed by IOC Container in Spring Framework.
Rules and Regulations to write Bean classes:
a) Bean classes must be POJO classes, they must not extend or implement any
predefined Library except java.io.Serializable marker interface.
b) Bean must be declared as "public" , "Non-abstract" and "non-final".
----> The main intention of declaring bean class as "public" is to make available bean
class scope to IOC Container inorder to create objects.
----> The main intention to declare bean class as "Non-abstract" is to allow to create
object .
----> The main intention to declare bean classes as "Non-final" is to extend one bean
class to another bean class inorder to improve reusability.
c) In Bean classes, we have to declare all properties as "private" and all behaviours as
"public", it will improve "Encapsulation".
d) If we want to provide any constructor in bean class then provide a constructor , it must
be 0-arg constructor and "public" constructor, because, IOC Container will search and
execute public and 0-arg constructor while instantiating bean.
If we want to use Beans in Spring applications then we must configure that bean classes
in spring beans configuration file , because, IOCContainer will recognize and create Bean
objects by getting bean class details from beans configuration file only.
There are three ways to provide beans configurations in spring applications.
- XML Configuration
- Java Based Configuration
3.Annotations Configuration
- XML Configuration
To provide beans configurations in beans configuration file we have to use the following
xml tags.
<beans>
<bean id="--" name="--" class="--" scope="--">
<property name="--" value="--"/>
</bean>
</beans>
Where <beans> tag is root tag in beans configuration file.
Where <bean> tag is able to provide configuration details of a particular bean class.
Where "class" attribute in <bean> is able to provide fully qualified name of the bean
class.
Where <property> attribute is able to represent a particular property[variable] in bean
class and it will set the specified value to the respective bean property by executing
setXXX(-) method.
Q)What is the difference between "id" attribute and "name" attribute in <bean> tag?
Answer: d' attribute is able to take exactly one identity to the bean object, it will not allow
more than one identity.
'name' attribute in <bean> tag is able to allow more than one identity name to the bean
object, where in multiple values only first value is treated as th actual bean identity and
the remaining names are alias names for the bean object. In this context, while providing
alias names to the bean object we have to use either ',' or ';' or [space] as
delimiter[seperator].
EX1
<beans>
<bean id="bean1" class="com.durgasoft.beans.MyBean"/>
</beans>
MyBean mb=(MyBean)ctx.getBean("bean1");
Status: Valid.
EX2
<beans>
<bean id="bean1 bean2 bean3" class="com.durgasoft.beans.MyBean"/>
</beans>
MyBean mb=(MyBean)ctx.getBean("bean1");
Status: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean
named 'bean1' is defined
Note: SImilarily the following cases are also be invalid.
<bean id="bean1,bean2,bean3" class="MyBean"/>->INvalid
<bean id="bean1;bean2;bean3" class="MyBean"/>->Invalid
<bean id="bean1bean2bean3" class="MyBean"/>-> Valid
EX3
<beans>
<bean name="bean1" class="MyBean"/>
</beans>
MyBean mb=(MyBean)ctx.getBean("bean1");
Status: Valid
EX4
<bean name="bean1 bean2 bean3" class="MyBean"/>
MyBean mb1=ctx.getBean("bean1");
MyBean mb2=ctx.getBean("bean2");
MyBean mb3=ctx.getBean("bean3");
Status: Valid
Similarily the following cases are also be valid.
<bean name="bean1,bean2,bean3" class="MyBean"/>
<bean name="bean1;bean2;bean3" class="MyBean"/>
Note: It is possible to use both 'id' attribute and 'name' attribute in single <bean> tag.
EX5
<bean id="bean1" name="bean2" class="MyBean"/>
MyBean mb1=ctx.getBean("bean1");
MyBean mb2=ctx.getBean("bean2");
Status: Valid.
Note: It is possible to provide bean alias names explicitly from out side of the bean
definition in configuration file by using <alias> tag.
<alias name="--" alias="--"/>
Where "name" attribuite will take bean logical name which we specified with "id"
attribute in beans configuration file.
Where "alias" attribute will take alias name.
EX6
<beans>
<bean name="bean1" class="MyBean"/>
<alias name="bean1" alias="bean2"/>
<alias name="bean2" alias="bean3"/>
</bean>
</beans>
MyBean mb1=ctx.getBean(bean1); --> Valid
MyBean mb2=ctx.getBean(bean2); --> Valid
MyBean mb3=ctx.getBean(bean3); --> Valid
- Key ideas of Spring - ApplicationContext explained simply
- Ready-to-use code examples
- Exam-style questions at the end