Tuesday, 20 August 2019

Spring - Session-1

Spring - Session-1
Video Link https://youtu.be/3oJ_gAbgDcs
===================================
About Spring Framework:

Developer: "developed by Rod Johnson in 2003"

Org: Spring Organization

Initial Name: Interface 21

Some Important Points About Spring Framework:
========================================================================
1. Spring is a lightweight framework (using Runtime Polyporphism, HAS-A).

It is main alternative of EJB Application which is Heavy Weight and Application Server Dependent.

2. Main advantages of Spring frameword is it provides "Loosly Coupled" approach - while
EJB provides "Tightly Coupled" which is NOT much suitable for programming.

========================================================================

Spring Module/Containers
============================
1. IoC (Inversion of Controlling) Module, contains the following:
---------------------------------------------------------------------------
1. Core Container - Interface name is: BeanFactory ------XmlBeanFactory (Implementation class)
2. J2EE Container - Interface name is: ApplicationContext ------ConfigurableApplicationContext(Child Interface)----ClassPathXmlApplicationContext (Implementation class)

The main job of container is : reads the data from xml file and pass to POJO class.

2. MVC Module, contains the following:
------------------------------------------------------------
1. Web Container - Interface name is: WebApplicationContext  --- WebApplicationContextUtil (Implementation class). It is a Factory class like DriverManager class.

IoC Container:
==============================================================
Same like ServletContainer It performs the following operations:
1. Read xml file
2. create instances of xml POJO
3. It will manage the life cycle of POJO
4. Dynamic parameter supply to POJO - this concept is known as dependency injection


How to start these Container or classes like tomcat container:
=================================================================
Main class or Driver class - that will drive/start the whole application, like:
class Test {
public static void main(String args[]) {
new XmlBeanFactory();// to start core container
new ClasspathXmlApplicationContext();//to start j2ee container
new WebApplicationContextUtil();// to start web container
}
}

========================================================================
First Spring Application:

We need the following components:
========================================
1. POJO class
2. xml file
3. Driver/Main class


Let's start from POJO class:
==============================================
public class Helloworld {
public void sayHello() {
System.out.println("Hello world Spring Application");
}
}
===============================================================


spring.xml - file
===============================================================
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<bean id="hw" class="beans.HelloWorld"/>
</beans>

NOte: Resource r = new ClassPathResource("resources/spring.xml");//It is like File class in IO.
It is used to load the xml file.


================================================================
Main/Driver class
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.HelloWorld;

public class Client2 {

public static void main(String[] args) {
//find and load xml file
//eager loading - pre
ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring.xml");

Object ob1=context.getBean("hw");
Object ob2=context.getBean("hw");
Object ob3=context.getBean("hw");
Object ob4=context.getBean("hw");
System.out.println(ob1);
System.out.println(ob2);
System.out.println(ob3);
System.out.println(ob4);


}

}


=======================================================================

IDE:
-------------------
1. Eclipse
2. MyEclipse
3. STS
4. NetBeans
5. RAD

========================================================================
scope of Object/POJO class object

1. Singleton (bydefault)
2. Prototype - for each request creates new object.
for Prototype approach we need to change the xml file:

Case 1 - If you are using default DTD: then
<bean singleton="true">


Case 2 - If you are using 2.0 or above DTD then
<bean scope="singleton or prototype">


xml files contains 5000 POJO class enteries:
BeanFactory class - User1 - 5 mins
User1 - 5 mins.




ApplicationContext 5000 class object will create at deployment time


https://youtu.be/3oJ_gAbgDcs

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...