Saturday, 24 February 2018

4.0 Dependency Injection

































Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container "injects" objects into other objects or "dependencies". 
  
Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container. 


Dependency injection - We want to drive car , we are only dealing with the intent(interface - object factory). Implementation doesn't matter for us. Mini car , micro car, prime car are the dependency injections which applicationContext.xml is injecting to our objectFactory.


There are two types of injection in Spring

  1. Constructor Injection
  2. Setter Injection

CONSTRUCTOR INJECTION
1. Define a dependency interface and class
2. Create a constructor in your class
3. Configure the dependency injection in config file.





















-------------------------------------------------------------------------------------

Interface.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public interface Swap {

public void doSwap(int a,int b);
public void anotherLogic(int a, int b);
}



-------------------------------------------------------------------------------------
FirstMethodSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class FirstMethodSwap implements Swap{

private Logic logic;
public FirstMethodSwap(Logic logic){
logic=this.logic;
}
public void doSwap(int a, int b) {
int c;
c=a;
a=b;
b=c;
System.out.println("Swap using 3rd variable : a = "+a+" and b= "+b);
}

public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}

}


-------------------------------------------------------------------------------------
SecondMethodSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class SecondMethodSwap implements Swap{

private Logic logic;
public SecondMethodSwap(Logic thelogic){
logic=thelogic;
}
public void doSwap(int a, int b) {
a=a+b;
b=a-b;
a=a-b;
System.out.println("Swap using 2 variable : a = "+a+" and b= "+b);
}

public void anotherLogic(int a, int b) {
logic.anotherLogic(a,b);
}

}



-------------------------------------------------------------------------------------
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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<constructor-arg ref="anotherSwap" />
</bean>



</beans>



-------------------------------------------------------------------------------------
Logic
-------------------------------------------------------------------------------------
package com.spring.demo;

public interface Logic {
public void anotherLogic(int a,int b);

}


-------------------------------------------------------------------------------------
AnotherLogic.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class AnotherLogic implements Logic{

public void anotherLogic(int a, int b) {
a=a*b;
b=a/b;
a=a/b;
System.out.println("Swap using 3rd variable one more logic : a = "+a+" b = "+b);
}

}


-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(4, 5);
context.close();

}

}




-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 2:11:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy
Feb 25, 2018 2:11:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
Feb 25, 2018 2:11:01 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy





SETTER INJECTION
1. Define a dependency interface and class
2. Create a setter method
3. Configure the dependency injection in config file.





















In the above mentioned codes do the below changes




-------------------------------------------------------------------------------------
DontSwap.java (add this new class)
-------------------------------------------------------------------------------------

package com.spring.demo;


public class DontSwap implements Swap {


private Logic logic;

public DontSwap(){
System.out.println("DontSwap - inside no-arg construtor");
}
public void doSwap(int a, int b) {
System.out.println("I don't swap : a = "+a+" b = "+b);
}

public void setLogic(Logic logic) {
System.out.println("DontSwap : inside setLogic setter");
this.logic = logic;
}
public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}


}


-------------------------------------------------------------------------------------
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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<property name="logic" ref="anotherSwap"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(3, 5);
Swap obj1=(Swap) context.getBean("Dontswap", Swap.class);
obj1.doSwap(3, 5);
obj1.anotherLogic(3, 5);
context.close();

}

}



-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 2:11:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy
Feb 25, 2018 2:11:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg constructor
DontSwap : inside setLogic setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
Feb 25, 2018 2:11:01 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy


























LITERALS SETTER INJECTION




-------------------------------------------------------------------------------------
DontSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class DontSwap implements Swap {

private String email;
private String name;

private Logic logic;

public DontSwap(){
System.out.println("DontSwap - inside no-arg construtor");
}
public void doSwap(int a, int b) {
System.out.println("I don't swap : a = "+a+" b = "+b);
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
System.out.println("DontSwap - inside setEmail setter");
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("DontSwap - inside setName setter");
this.name = name;
}
public void setLogic(Logic logic) {
System.out.println("DontSwap : inside setLogic setter");
this.logic = logic;
}
public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}



}


-------------------------------------------------------------------------------------
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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<!-- Constructor injection -->
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<!-- Setter injection -->
<property name="logic" ref="anotherSwap"></property>
<!-- literals injection -->
<property name="email" value="bikesh.kumar@coder.com"></property>
<property name="name" value="Bikesh Kumar"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(3, 5);
Swap obj1=(Swap) context.getBean("Dontswap", Swap.class);
obj1.doSwap(3, 5);
obj1.anotherLogic(3, 5);
DontSwap obj2=(DontSwap) context.getBean("Dontswap",DontSwap.class);
System.out.println(obj2.getEmail());
System.out.println(obj2.getName());
context.close();

}

}


-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------Feb 25, 2018 3:10:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:10:32 IST 2018]; root of context hierarchy
Feb 25, 2018 3:10:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg construtor
DontSwap : inside setLogic setter
DontSwap - inside setEmail setter
DontSwap - inside setName setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
bikesh.kumar@coder.com
Bikesh Kumar
Feb 25, 2018 3:10:32 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:10:32 IST 2018]; root of context hierarchy






LITERALS SETTER INJECTION  USING PROPERTY FILE





















-------------------------------------------------------------------------------------
bikesh.properties
-------------------------------------------------------------------------------------
foo.email=bikesh.kumar@coder.com
foo.name=Bikesh Kumar


-------------------------------------------------------------------------------------
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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<!-- load properties file : bikesh.properties -->
<context:property-placeholder location="classpath:bikesh.properties" />

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<!-- Constructor injection -->
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<!-- Setter injection -->
<property name="logic" ref="anotherSwap"></property>
<!-- literals injection -->
<property name="email" value="${foo.email}"></property>
<property name="name" value="${foo.name}"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 3:19:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:19:57 IST 2018]; root of context hierarchy
Feb 25, 2018 3:19:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg construtorf
DontSwap : inside setLogic setter
DontSwap - inside setEmail setter
DontSwap - inside setName setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
bikesh.kumar@coder.com
Bikesh Kumar
Feb 25, 2018 3:19:58 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:19:57 IST 2018]; root of context hierarchy

Read More »

Thursday, 22 February 2018

3.0 Inversion of Control (IoC)

IoC means - Design construction and management of the object (Object outsourcing for the construction of object)

IOC - Inversion of control
It is an architectural pattern which tells that programmer don't need to create any object instead describe how it should be created in an xml or file.
Object creation and its life cycle will be manage by spring container. This is called inversion of control.

It is a programming model use to develop objects of an application so that they can be reused and managed.

Tight coupling code is difficult for maintenance
A---->depend on ------->B
IOC is a programming model which state that resources required by object for performing its operation must not created by the object itself rather they must be provided at runtime.

To put the values in reference variable to remove the dependency - Spring use dependency injection.

Object doesn't ask for the resources rather it is provided.

Don't call me,I'll call you.

IOC is a design pattern which is implemented by DI


YOUTUBE VIDEO TUTORIAL - by AndroJava Tech4U






Primary function of the Spring container

  1. Create and manage  of object (Inversion of Control)
  2. Inject Object's dependency (Dependency Injection)

There are three ways for  Configuring Container
  1. xml configuration file
  2. Java Annotations
  3. Java Source code

Spring Development Proces

1.Creating your spring beans


File : applicationContext.xml
<beans ...>
          <bean id="myCoach">
          </bean class="com.spring.demo.BaselballCoach">
</beans>

2.Create a spring container
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");

3.Retrieve beans from spring container
Coach obj=context.getBean("myCoach", Coach.class );


Spring container is generally known as ApplicationContext


First SPRING example

























Human.java
---------------------------------------------------------------------
package com.spring.demo;

public interface Human {
public String getMyType();
}


Men.java
---------------------------------------------------------------------
package com.spring.demo;

public class Men implements Human{
public String getMyType(){
return "I am a men";
}
}

Women.java
---------------------------------------------------------------------
package com.spring.demo;

public class Women implements Human{
public String getMyType(){
return "I am a Women";
}
}


MyApp.java
---------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {
public static void main(String args[])
{
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Human type1=context.getBean("myType1",Human.class);
Human type2=context.getBean("myType2",Human.class);
System.out.println(type1.getMyType());
System.out.println(type2.getMyType());
context.close();
}
}

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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">


  <bean id="myType1"
  class="com.spring.demo.Men">
  </bean>
 
  <bean id="myType2"
  class="com.spring.demo.Women">
  </bean>


</beans>

---------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------
Feb 23, 2018 10:44:53 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Fri Feb 23 10:44:53 IST 2018]; root of context hierarchy
Feb 23, 2018 10:44:53 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
I am a Women
Feb 23, 2018 10:44:53 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Fri Feb 23 10:44:53 IST 2018]; root of context hierarchy





Read More »

Wednesday, 21 February 2018

2.0 Project Setup

SPRING PROJECT SETUP

  1. Download Eclipse EEAA
  2. Download Tomcat
  3. Integrate Tomcat with Eclipse
  4. Download the Spring jar files (Spring Repository)

For all the above mentioned steps there are tons of video tutorial available on youtube.

1. Create a new java project 
2. Add all the spring jars in build path library


1. New---> Java Project

2.Project-name --> Next

3. Libraries -- Add library--> User Library

4. New -- > New user Library (Give any name)

5. Add external jars (copy all the jars form Spring zip (form lib folder))

6. Ok --> Finish





























Read More »

New features in Java 8

Java 8 New Features


  1. Lambda expressions,
  2. Method references,
  3. Functional interfaces,
  4. Stream API,
  5. Default methods,
  6. Base64 Encode Decode,
  7. Static methods in interface,
  8. Optional class,
  9. Collectors class,
  10. ForEach() method,
  11. Parallel array sorting,
  12. Nashorn JavaScript Engine,
  13. Parallel Array Sorting,
  14. Type and Repating Annotations,
  15. IO Enhancements,
  16. Concurrency Enhancements,
  17. JDBC Enhancements etc.
Read More »

1.0 Spring and Hibernate

Spring - lightweight alternative to J2EE with large number of helper classes.

Why Spring ?

EJB (Entity beans are more complex and  mapping between java classes and databases was affecting the performance). Spring simplify the codes.
It is a lightweight development with java POJOs(Plain-Old-Java_Objects).


ROD Johnson - Founder of Spring



















Release timeline
Spring 1.0 = 2004
Spring 2.0 = 2006
Spring 3.0 = 2009
Spring 4.0 = 2013
Spring 4.3 = 2016
Spring 5 = 2017


Because EJB in J2EE was slow spring became popular. Sun microsystem re engineered the EJB to use it easily but it was very late.

Which one we have to learn ? Spring or Java EE (Both can do same things)

Spring 5 need Java8 or higher

Official website of spring = www.spring.io

SPRING FRAMEWORK PRINCIPLE

Don't Call us we'll call you - Programmer don't need to worry about creating objects also known as dependency injection


















Read More »

Monday, 19 February 2018

2.0 JSP & SERVLETS (BEST PROJECT EXAMPLE)

MOVIE PROJECT (CRUD OPERATIONS + LOGIN + LOGOUT EXAMPLE)







































Download Project : Movie Project

Read More »