Class Notes
Class Notes
Class Notes
================
Pre-Requisites
--------------
1) Core Java
- java.lang package
- OOPS
- Inheritence
- Polymorphism
- Encapsulation
- Abstraction
- Collections
- List
- Set
- Map
- Properties
- Comparator & Comparable
- Enumeration, Iterator and ListIterator
- I/O Streams
- Exception Handling
- Multi Threading
- Annotations
- Generics
- Java 8 features
- Reflection API
Advanced Java
=============
JDBC API
- Driver
- DriverManager
- Connection
- Statement & Pstmt & Cstmt
- ResultSet
- DatabaseMetaData, ResultMetaData etc...
- Connection Pooling
Servlets
- Web Application
- WebServer & App Server
- Application Deployment process
- Servlet, ServletConfig, ServletContext
- Servlet Life Cycle
- ServletRequest, ServletResponse
- GenericServlet, HttpServlet
- RequestDispatcher
- HttpSession
- Filters & Listeners
JSP
- Why Jsp
- Jsp Life cycle
- Jsp Implicit objects
- directives, tags, custom tags etc..
- JSTL
Databases
---------
SQL knowledge
Webservice , Restful Services & Hibernate --- parallely you can learn.
============================================================
Introduction (7 days)
======================
What is Framework
Why we need frameworks
Types of Frameworks
Why Spring became very popular ?
Comparision Spring with Other frameworks
Advantages of using Spring in project
Spring Architecture
Spring Modules
Spring Tx (2 days)
================================================================
facebook group name: Ashok IT School
email id : [email protected]
-------------------------------------------------------------------
Java : Programming language. Providing synatxes for us. We can develop CUI and GUI
applications using this.
JEE
====
Servlets & JSP : These technologies are used to develop web applications.
Frameworks
===========
Hibernate : Hibernate is a ORM framework. To develop Persistence layer. It is
advanced to JDBC api.
Strutus 1./2.x : These are Java based web application frameworks. These are used to
develop web applications. (Out dated).
Distributed Technologies
==========================
Webservices & Restful Services : These are distributed technologies. Using these
technologies one application can communicate with another application.
What is framework ?
-------------------
Framework is a semi developed software which provides some common
logics which are required for several applications development.
Ex : Connection opening & closing, query execution, capture form data, sending
email, generating excel report, generating pdf report, sending msg to mobile, copy
data from one object to another object etc......
Advantages of frameworks
=========================
1) Code Re-Usability
Dis-Advantages
===============
1) When we use frameworks in project development, performance of application will
be bit slow.
Types of Frameworks
-------------------
1) Web frameworks : Used to develop only web components
Ex : Spring
Layered architecture
--------------------
Now a days every project is getting developed using layered architecture.
Below are the layers that we can see in real time projects.
----------------------------------------------------------
1) Presentation Layer
2) Web Layer
3) Service Layer
4) Persistence Layer
1) Presentation Layer :
----------------------
Presentation layer contains user interface (UI)
Ex : Login form, registration, forgot password, search etc...
2) Web Layer :
--------------
1) In this layer we will develop web components.
2) Web components are also called as Request Handlers.
3) Request Handlers are also called as Controllers in frameworks.
3) Persistence layer
--------------------
a) This layer contains persistent components
b) Persistent components are responsible for only Database communication.
c) Persistent components are called as dao s or repositories.
Pivotal team
3) Spring will not force you to use all modules. Whatever is required for project
take only that module from spring.
Note: Struts will force programmers to extend properties from Action class hence
Struts is called as Invasive framework.
6) Spring supports POJO and POJI model programming. Our classes will be loosely
coupled with Spring Framework. In future if we don't want to use spring just we
need to remove spring jars from build path and no need to touch source code.
Note: If we use struts framework, our application classes will be tightly coupled
with struts framework. Removing Struts framework from the project is not easy bcz
every class is tightly coupled with Action class.
Spring Architecture
-------------------
Spring architecture changed from version to version.
Intial Version of Spring is 1.x. It contains total 7 modules.
Spring 3.x contains almost 20 modules. (So many annotations introduced) in 3.x
version.
Note : In Spring 2.x version Spring Web and Spring Web MVC are combined as Single
module hence we have total 6 modules in Spring 2.x version.
Spring Core
-----------
-> This is base module for spring framework.
DI : Dependency Injection.
-> All other modules in spring are depeloped on top of spring core module.
Spring Context:
--------------
-> Spring context module deals with Configuration in Spring applications.
Spring DAO:
-----------
-> DAO means data access object.
-> It is used to develop persistence layer using spring framework.
-> This is also called as Spring JDBC.
-> This Spring DAO module is developed on top of java jdbc.
-> This module resolved boiler plate code writing.
-> In ORM frameworks also we need to write some boiler plate code.
-> To avoid boiler plate code in ORM frameworks, spring provided Spring ORM module.
-> Spring ORM module will support for several orm frameworks.
1) Primary logic
2) Secondary logic
-> If we write both primary and secondary logics together then tightly coupling
problem will occur hence it is not recommended.
-> To seperate primary and secondary logics we will use Spring AOP module in
project.
-> Spring web MVC module will use FrontController design pattern to process client
requests.
-> Spring MVC supports for multiple view technologies like html, jsp, theamleaf,
velocity and freemarker etc..
-> Spring MVC supports for form binding object. Form data can be binded to model
objects automatically.
Spring Core
-----------
-> This is base module of Spring framework
Based on role the classes are playing they can be divided into below 3 types
1) POJO
2) Java bean
3) Component
POJO classes shouldn't extend or implement any api or framework related classes and
interfaces.
These classes are used to transfer data from one layer to another layer in object
format.
Ex:
---
Java Bean
----------
The class which follows bean specification rules is called as java bean class.
Rules
-----
1) Class should be public
2) Class should contain public 0-param constructor.
3) Every variable should be private
4) Every variable should contain public setter & getter methods
5) Recommended to implement java.io.Serializable interface.
- > Java Bean classes are used to exchange data among multiple layers.
public User(){
}
Component
---------
The classes which contains business logic are called as Component classes.
When we have several classes like above, all the classes are dependent on some
other classes to complete execution.
1) IS-A (Inheritence)
2) HAS-A (Composition)
Inheritence
-----------
Extending properties from one class to another class is called inheritence
Dis-Advantages
--------------
-> Classes will be tightly coupled
-> Our class can't extend properties from any other class in future
Composition
-----------
The process of creating object for a class.
Composition Dis-Advantages
----------------------------
1) Classes are tightly coupled
Inheritence and Composition concepts are used to talk from one class to another
class.
Calendar.newInstance() ; //
https://github.com/Ashok-IT-School/26-Spring-11AM
class Demo{
public static void display(ArrayList al){
//logic
sysout(al);
}
}
---------------------------------------------------------------
class Test{
psvm(String... ar){
LinkedList ll = new LinkedList();
ll.add(10); ll.add(20); ll.add(30);
Demo.display(ll);
class Demo{
public List getData(){
//logic
ArrayList al = new ArrayList();
al.add(20);
return al;
}
}
------------------------------------------------------
List list = getData();
The process of injecting dependent object into target object is called Dependency
Injection (DI).
Setter Injection : Injecting dependent object into target object by calling target
class setter method.
Ans) No - If your classes folollow some rules and guidelines then only ioc can
manage and colloborate your objects.
If we need to use IOC for Dependency Injection for our classes then our classes
should follow Strategy Design Pattern.
3) Code should be open for extension and should be closed for modification.
Spring Environement Setup
-------------------------
Softwares Required
------------------
1) Maven
2) IDE
3) JDK 8 or higher
4) Spring jars
5) Commons-Logging jar
1)BeanFactory
2)ApplicationContext
ClassPathResource
FileSystemResource
String fileName="Beans.xml";
Resource resource = new ClassPathResource(fileName);
BeanFactory factory = new XmlBeanFactory(resource);
----------------------------------------------------------------
If xml follows above 2 rules then we can say that xml is well-formed. If xml is
wellformed humans & machines can read that xml.
<person>
<id>101</id>
<name>Raju</name>
<adress>
<city>Hyd</city>
<state>TG</state>
</adress>
</person>
--------------------------------------------------------------
<order>
<item>
<code>101</code>
<quantity>abc</quantity>
</item>
<address>
<city>Hyd</city>
<state>TG</state>
<zip>500081</zip>
</address>
</order>
======================================================================
Xml
---
1) Simple Element
<id>101</id>
2) Compound Element
<name>
<first-name>Cedrone</first-name>
<last-name>Charles</last-name>
</name>
-----------------------------------------------------------------
--------JAX-P API-------------------
SAX
DOM
STAX
----------------------------------------------
------------------------Beans.xml------------------------
<beans>
</beans>
----------------------------------------------------------
ClassPathResource
FileSystemResource
------------------------------------------------------------------
1) BeanFactory
Resource resource =
new ClassPathResource("com/maruti/cfg/Beans.xml);
BeanFactory factory =
new XmlBeanFactory(resource);
---------------------------------------------------------------------
4) BeanFactory will load all beans defintions from xml file and
will store BeansMetaData (in memory).
----------------------------------------------------------------------
factory.getBean("car");
Object obj = factory.getBean("paymentCtx");
PaymentContext ctxt = (PaymentContext) obj;
-------------------------------------------------------------
PaymentContext ctxt = (PaymentContext) factory.getBean("paymentCtx");
-------------------------------------------------------------------PaymentContext
ctxt =
factory.getBean("paymentCtx",PaymentContext.class);
--------------------------------------------------------------------
Constructor Injection:
----------------------
INjecting dependent class object into target class by calling
target class constructor is called as Constructor Injection(CI).
------------------------------------------------------------------
<bean id="car" class="pkg.Car">
<constructor-arg name="eng" ref="petrolEng" />
</bean>
----------------------------------------------------------------
setter method : setter injection
class Person {
}
----------------------------------------------
<bean id="person" class="pkg.Person">
<property name="personId" value="101" />
<property name="personName" value="John"/>
<property name="personAge" value="25"/>
</bean>
PaymentContext(IPayment payment){
System.out.println(p); //com.hps.beans.Person@2ed94a8b
System.out.println(p.toString());com.hps.beans.Person@2ed94a8b
-================================================
return
this.getClass().getName()+"@"+Integer.toHexString(this.hashCode());
com.hps.Person@e979l96
------------------------------------------------------------------------
Collection Injection
--------------------
List
Set
Map and
Properties
<property name="places">
<list>
<value>Hyd</value>
<value>Pune</value>
</list>
</property>
-------------------------------------------------------
private Set<Long> phnos; //java.util.LinkedHashSet
<property name="phnos">
<set>
<value>0808080</value>
<value>9797979</value>
</set>
</property>
--------------------------------------------------------
private Map<String,Long> projectCodes; //java.util.LinkedHashMap
<property name="projectCodes">
<map key-type="" value-type="">
<entry key="" value="" />
<entry key="" value""/>
</map>
</property>
---------------------------------------------------------------------
<property name="emails">
<props>
<prop key="personal">iyiy</prop>
<prop key="ofc"></prop>
</props>
</property>
------------------------------------------------------------------
Enumeration
Iterator
ListIterator
SplitIterator (jdk 1.8)
Movie.java
----------
Note: In Constructor Injection, its mandatory that we need to inject values for all
the variables.
If we don't know value for a variable, then we shuld use null injection.
<constructor-arg name="movieName">
<null />
</constructor-arg>
Bean Scopes
-----------
Bean Scope will decide how many objects should be created for a bean class in IOC
container.
syntax :
-------
<bean id="car"
class="pkg.Car"
scope="singleton|prototype|request|session" />
Prototype : For every call of getBean() method new object will be created.
factory.getBean("movie",Movie.class); //
singleton
prototype
request
session
BeanFactory
-----------
It is an interface
It is used to start IOC Container
IT is Lazy Container
Why it is called as Lazy ? :: Until unless we call getBean( ) method bean object
will not be created.
how many objects should be created for a bean depends on bean scope.
ApplicationContext
------------------
It is an interface
It is also used to Start IOC Container
It is Eager container
Why it is Eager Container ? :: When IOC starts immediatley it will create bean
objects if scope is singleton and lazy-init=false.
It supports I18N
It supports Event Handling
It supports Web Application Development
ApplicationContext ctx =
new ClasspathXmlApplicationContext(String filePath)
Bean Inheritence
----------------
What is Inheritence?
--------------------
Bean Inheritence
----------------
To copy properties from one bean to another bean we can use Bean Inheritence
concept in Spring.
Note: If property is not declared in child bean definition then only IOC will copy
parent bean property value to child bean property.
Collection Merging
------------------
If we want to copy collection properties from one bean to another bean then we can
use Collection Merging.
-----------------------------------------------------------------------
Meeting :
id = 101
name = Triage Meeting
purpose = To discuss defects occured in UAT
participants = john, Ram, Sita, Gita
Scrum Meetings
Status Meetings
Triage Meetings
Manual Wiring
------------
Injecting dependent object into target object using ref attribute.
AutoWiring
----------
IOC can identify dependent object and it can inject into target object.
byName:
-------
With target class user-defined variable name there should be a bean in bean
configuration file. Then that bean will be considered as dependent bean and it will
be injected to target bean.
If we don't have any bean defintion which is matching with target class user
defined type variable then autowiring can't be performed.
Note : IOC will check varibale name with bean id or bean name.
byType:
-------
IOC will check user-defined varible data type with bean class in bean configuration
file. If variable data type and Bean class is same then ioc consider that bean as
dependent bean and it will inject that bean to target.
Note : There is a possibility that one class can be configured with multiple bean
definitions using unique ids. In this sitatuion IOC can't identify dependent bean
hence it will throw Exception (NoUniqueBeanDefinitionFound).
To resolve this we can use 'primary' attribute in bean defition. The bean which
contains primary=true is called as auto-wire candidate.
byName - variable name should match with bean name
constructor
constructor
-----------
What is Spring
Spring Advantages
Spring Modules
IOC
Dependency Injection
Setter Injection
Constructor Injection
Setter Injections vs Constructor Injection
BeanFactory
ApplicationContext
Injecting Inner Beans
Collection Injection
Bean Scopes
Bean Inheritence
Collection Merging
Autowiring
----------------------------------------------------------------------
IOC
DI ( SI & CI )
<bean id=""
class=""
scope=""
parent=""
abstract=""
lazy-init=""
autowire="" >
<property />
<constructor-arg />
</bean>
<bean id=""
class=""
scope=""
parent=""
abstract=""
lazy-init=""
autowire="" >
</bean>
-----------------------------------------
class Contact {
int contactId;
String contactName;
Long contactNumber;
Address addr;
//Setter methods
//toString( )
}
----------------old approach---------------------------
<beans <xsd linking> >
<bean id="c" class="pkg.Contact">
<property name="contactId" value="101" />
<property name="contactName" value="Raj"/>
<property name="contactNumber" value="797979979"/>
<property name="addr" ref="a"/>
</bean>
</beans>
---------------------using p-namespace-----------
<beans xsd-linking p-namespace-url >
Depends-On
----------
class Car {
class Robot {
}
-------------------------------------------------------------
<bean id="c" class="pkg.Chip" />
Bean Aliasing
=============
Note : When we configure multiple names for a bean using name attribute space and
comma will be considered as delimter.
If you want write a bean name including comma or space then we should use alias
tag.
-------------------------------------------------------------------------
Amazon - E-Commerce
BlueDart
<bean id="e1" class="pkg.Engine" />
------------------------------------------------
p2
p3
p4
p5,
Bean Aliasing
-------------
id vs name
ApplicationContext
------------------
Aware Interfaces
------------------
This concept is also called as Interface Injection.
If IOC is injected to our target class, then target class can decide which
dependent object it should load in runtime based on conditions.
BeanFactoryAware
----------------
//logic
}
Factory Methods
---------------
The method which is responsible to creat same or different class object is called
as factory method.
Calendar c = Calendar.getInstance();
char ch = msg.charAt(0);
class Car {
-------------------------------------------
Calendar c = Calendar.getInstance( ) ;
12-May-2019 + 57 = ?
new Date()
SimpleDateFormat
----------------
Date parse(String str) -----> String to Date conversion
web components
service components
persistent components
Method Injections
-----------------
1) LookUp method Injection : When we want to inject prototype bean into singleton
bean
tg.getToken(); // 1234
tg.getToken( ); //1234