Spring Framework
- Spring is one of the most popular frameworks for Java Enterprise Edition (EE).
- Spring Framework is used for developing Reliable and High-Quality Applications.
- Spring Framework is a light weight framework.
- Spring Framework was designed by Rod Johnson.
- Spring Framework is a based on several Modules like: Spring Core, Spring Context, Spring Web-Mvc, Spring Aop, Spring Jdbc, Spring Security etc.
- Using Spring Framework you can acheive loose coupling, easy testing etc.
- Spring is based on Configuration.
- You can acheive Configuration using xml or annotation.
What is IOC Container ?
Spring IOC container is the core part of Spring Framework.
IOC container is responsible to create instance of bean.
IOC container is responsible to manage the life cycle of the bean.
IOC container is responsible to configure the object.
IOC container is responsible to assemble dependencies between the objets.
IOC container is responsible to convert values to java type using Spring Expression Language (SpEL).
Spring provides following two types of containers.
- BeanFactory
- ApplicationContext
BeanFactory
The BeanFactory API provides the underlying basis for Spring’s IOC functionality.
Its mostly used in integration with other parts of Spring and related third-party frameworks.
Its the root interface for accessing a Spring bean container.
ApplicationContext
It is a sub class of ListableBeanFactory and ListableBeanFactory is a sub class of BeanFactory.
So ApplicationContext includes all the functionality of a BeanFactory.
BeanFactory vs ApplicationContext
BeanFactory | ApplicationContext |
---|---|
BeanFactory supports only singleton and prototype scope. | ApplicationContext container does support all the bean-scopes. |
BeanFactory supports lazy initialization. | ApplicationContext support Eager initialization. |
BeanFactory does not supports internationalization (I18N). | ApplicationContext supports internationalization (I18N) for text messages. |
BeanFactory does not supports event publication. | ApplicationContext supports event publication to the registered listeners. |
BeanFactory annotation based dependency injection is not supported. | ApplicationContext supports annotation based dependency injection. |
BeanFactory implemented class XmlBeanFactory. | ApplicationContext implemented class ClassPathXmlApplicationContext, FileSystemXmlApplicationContext and WebApplicationContext. |
BeanFactory does not supports to load multiple configuration files. | ApplicationContext supports to load multiple configuration files. |
BeanFactory is used where resources are limited like mobile. | ApplicationContext is used where resources are not limited. |
How to create BeanFactory ?
First Way:
InputStream in = new FileInputStream("spring_config.xml"); BeanFactory demo = new XmlBeanFactory(in); //Get bean from configuration xml file HelloWorld obj = (HelloWorld) demo.getBean("hello");
Second Way:
//lookup from system location Resource res1 = new FileSystemResource("spring_config.xml"); BeanFactory demo1 = new XmlBeanFactory(res1);
Third Way:
//lookup from classpath location ClassPathResource res2 = new ClassPathResource("spring_config.xml"); BeanFactory demo2 = new XmlBeanFactory(res2);
How to create ApplicationContext ?
First Way:
//lookup from classpath location ApplicationContext ac=new ClassPathXmlApplicationContext("spring_config.xml"); HelloWorld obj = (HelloWorld) ac.getBean("hello");
Second Way:
//lookup from system location ApplicationContext ac=new FileSystemXmlApplicationContext("spring_config.xml"); HelloWorld obj = (HelloWorld) ac.getBean("hello");
Third Way:
//lookup from web location WebXmlApplicationContext for within the web application.
What is Dependency Injection ?
Dependency Injection is a part of IOC container.
Dependency Injection means how an object inject by a container.
Dependency Injection is a design pattern using which removes the dependency from the programming code.
Dependency Injection provide loose coupling.
You can acheive Dependency Injection by two way :
- By setter method
- By constructor
Bean Scopes
When you create a bean definition, that means you can create actual instance of the class define by the bean definition.
You can create one or many instances from a single scope.
By default bean scope is singleton.
The Spring Framework supports six scopes, four of which are available only if you use a web-based ApplicationContext.
Beans can be defined to be deployed in one of a number of scopes.
There are follwoing Beans Scopes:
Scope | Descriptions |
---|---|
singleton | Single Instance for each Spring IOC container. |
prototype | Multiple Instance for each Spring IOC container. |
session | Single scope bean definition for life cycle of Http Session (HttpSession). |
request | Single scope bean definition for life cycle of Http Request (HttpServletRequest). |
application | Single scope bean definition for life cycle of application scope (ServletContext). |
websocket | Single scope bean definition for life cycle of WebSocket. |
Bean Definition Inheritence
It same just like Inheritence but is case of Spring nothing to do with java class like extends etc.
In case of Inheritence child bean can access the property of parent bean and also can be override.
Child bean can also add some new field in own class.
In case of Inheritence in Spring Framework we use parent attribute in child bean definition.
No comments:
Post a Comment