spring is an application development framework
by using spring frame work we can develop end to end application
spring is not a single frameworks .it is a collection of frameworks
spring framework developed in modular fashion
several modules are availabel in spring frame work
1. spring core 2 spring context 3. spring AOP 4. spring DAO 5. spring web MVC
6 .spring ORM
spring is versitile framework. spring can be intregated with any other framework
which is available in the market
spring is non-invasive framework.spring will not force the programmer to
extend/implement any framework relaeated classes and interface
spring is loosely coupled framework
spring core is base module for spring framework this module providing fundmental
concepts of spring they are IOC and DI
spring context module will take care of configurations required our applications
spring AOP module is Used to seperate business logic and secondary logic in our
application
Spring DAO/Spring JDBC module is used to develop persistance layer . Spring Jdbc
Module is developed on of JDBC API
spring web MVC module is used to develop web applications
Spring ORM module is used to Develop persistance layer Using ORM principles
Spring frame work developed by rod johnson
versions
----==
2002----0.9
2004--->1.0(first production released)
2006===>2.0
2009===>3.0
2013====>4.0
2017===>5.0 to current version
in spring 1.x version 7 modules are available
in spring 2.x version total 6 modules
spring web and spring MVC modules combinded released as single module
in spring 3.x version each module is categorized in several sub modules
in spring 4.x version messaging concept added
in spring 5.x version recative programming concept added
spring boot is another approach to develop spring based applications with minimal
configurations
spring boot is mainly used to develop Rest APIs
the primary feature of spring boot is auto configuration .it automatically
configures the classes based on the requirment
spring boot offers embeded servers such as jetty and tomcat...etc
steps to develop sprig boot application
----------------------------------------------
1.goto initial website start .spring.io.
in our application several classes will be available
2.one class method wants to talk another class method
3.we can establish communation among the classes in 2 ways
1. inheritance 2. composition
4.if we use inheritance or composition then classes will be tightly coupled
5. instead of we are creating objects we ask spring core to manage dependencies
among our classes
note:-if we want spring core module to manage dependencies among the classes with
loosely
coupling then we have to develop our classes by following some best practicies
6. "spring core "suggesting developers to follow "stragey design pattern" to
develop classes
so that "spring core" can easily mange dependacies among the classes with loosely
coupling
stragery design pattern
-----------------------------
1. it comes under behavirioal design pattern
2. it eables our code to choose an algoritham at run time
3. when we have multiple algolirithams then we can use this pattern to chhose one
algoritham at run time
4. it will make our classes loosely coupled
rules
------
1. favor composition our inheritance
2. code to interace instead of implementation classes
3. code should be open for extension and code closed for modification
dependancy injection
-------------------------
the process of injectiong one class object into another class object is called as
dependancy injection
we can perform dependancy injection in 3 ways
1. setter injection
2. constructor injection
3. field injection
setter injection
------------------
the process of injecting one class object into another class object using setter
method then it is clled as setter injection
ex:-BillCollector bc=new BillCollector();
bc.setPayment(new CreditcardPayment());
constructor injection
-----------------------
the process of injctiong one class object into another class object using
constructor is class as
constrcutor injection
Billcollector bc1=new BillCollector(new CreditCardPayment());
field injecton
----------------
the process of injectiong one class object into another class object using
varibale is called as
fileld injection
NOTE:
------
if the variable is declared as public than we can access that variable ouside the
class we can initiliaze directly
if the varaibale is declared as private then we cannot access that variable
outside of the class directly
to access private varibales ouside of the class we can use reflection API
if we develop the project without using spring frame work then programmer should
perform dependency injection among the classes in the application
if the programmer performs dependancy injection then classes will become tightly
coupled
in real time project we will have 100's of classes then performing dependanccy
injection is very very difficult and code will be confisued.
if we develop the project using spring frame work then spring IOC will take care of
dependancy injections in our application
to over come this problem we can use spring IOC container.
IOC is a principle which is responsible to mange and colloborate dependencies among
the classes available in the application
note:- IOC stands for Inversion of control DI stands for dependancy injection
in spring frame work IOC will perform DI
build first application using spring frame work
---------------------------------------------------------
1.open STS IDE and create maven project
2. add-spring context dependancy in project "pom.xml" file
file---->new ----->maven project
check mark a saple project
group-id====>com.orbit
artifact id=====>04-First-Spring-App
click on finish
mouse rigt clcik on artifact id choose build path and then choose configure build
path
under libraryes tab click on edit choose alternative jre=====>jre 1.8
now add JDK===>
clik on installed JRE's
click on add
click on directory----->c:\program file\java \jdk\1.8
click on finish
select the JDK click on apply
now open pom.xml file in our project
in crome browser type mvn repository
in that repository type spring -context
copy the dependencies and paste in pom.xml file
3.create requird classes in our application
Ipayment.java
CreditCardpayment.java
DebitCardPayment.java
UpiPayment.java
BillCollector.java
Test.java
note:-all above class managed by IOC container. we are following statragy design
pattern.
4. create beans configuration file
in project explorer expand spring project
under src/main/java mouse right clik on it file---->new--->choose spring bean
configuration file option
write the following code
-------------------------------------
<bean id="creditcard" class="com.orbit.CreditCardPayment"/>
<bean id="debitcard" class="com.orbit.DebitCardPayment"/>
<bean id="upi" class="com.orbit.UpiPayment"/>
<bean id="billcollector" class="com.orbit.BillCollector">
<property name="payment" ref="com.orbit.CreditCard"/>
</bean>
note- by using payment variable we need to call setter injection
5. start the IOC container and test the application
packege com.orbit;
class Test
{
public static void main(String args[ ])throws Exception
{
ApplicationContext context=new classPathXml(ApplicationContext("Spring-Beans.xml");
BillCollector bc=context.getBean("billcollector",BillCollector.class);
bc.collectPayment(1400.00);
}
}
in bill collector class write the following code to display package name
public class BillCollector
{
private Ipayment payment;
public void setPayment(Ipayment payment)
{
this.payment=payment;
}
public BillCollector()
{
}
Public BillCollector(Ipayment payment)
{
this.payment=payment;
}
public void collectPayment(double amount)
{
System.out.println(payment.getClass().getName());
String status =payment.pay(amount);
System.out.println(status);
}
}
note:- in the above example IOC container creating object not performing dependancy
injection.
in Spring-Beans.xml file write the following code to run constructor
<constructor-arg name="payment" ref="Upi"/>
write the follownig code in bill collector class
com.orbit;
public class BillCollector
{
private Ipayment payment;
public BillCollector(Ipayment payment)
{
System.out.println("BillCollector ::param constructor executed");
this.payment=payment;
}
public void collectPayment(double amount)
{
System.out.println("Injected::"+payment.getClass().getName());
String status=payment.pay(amount);
System.out.println(status);
}
}
note:- to perform setter injection we will use <property/>tag like below
<bean id="billcollector" class="com.orbit.BillCollector">
</bean>
to perform constructor injection we will use
<constructor-arg/?tag like below
<constructor-arg name="payment ref="Upi"/>
</bean>
bean scopes
--------------
bean scope will deside how many objects should be created for a spring bean
the default scope of spring bean is singleton(means only one object will be
created)
we can configure below scopes for spring bean
1. singleton
2.proto type
3. request
4. session
create marven project[Spring-Bean-Scopes-App]
add dependencies in pom.xml
in src/main/java create packege[com.orbit]
inside this pacage create one class[motor.java]
package com.orbit;
public class motor
{
public motor()
{
System.out.println("Motor::Constructor");
}
}
mouse right click on src/main/java create bean configuration file[Spring-Beans.xml]
in that file write the following code
<bean id="motor" class="com.orbit.motor"/>
mouse right click on package create a class[Test.java]
in that write the following code
package com.orbit;
class Test
{
public static void main(String args[ ])
{
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Beans.xml");
}
}
mouse rigth click on package create one more class[car.java]
in that write the following code
package com.orbit;
public class car
{
public car()
{
System.out.println("car::Constructor");
}
}
in bean configuration file[Spring-Beans.xm]
in that file write the following code
<bean id="motor" class="com.orbit.motor"/>
<bean id="car" class="com.orbit.car"/>
to get the objct from the IOC container write the following code in Test class
package com.orbit;
class Test
{
public static void main(String args[ ])
{
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Beans.xml");
Car car1=context.geBean("car",Car.class);
System.out.println(car1.hashCode());
Car car2=context.getBean("car",Car.class);
System.out.println(car2.hashCode());
}
}
note:-here only one object created for car class because by default every spring
bean is singleton
for singleton beans objects will be created when IOC starts
in bean configuration file[Spring-Beans.xml]
in that file write the following code
<bean id="motor" class="com.orbit.motor" scope="prototype"/>
<bean id="car" class="com.orbit.car"/>
in test class write the following code
package com.orbit;
class Test
{
public static void main(String args[ ])
{
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Beans.xml");
Motor motor1=context.geBean("Motor",Motor.class);
System.out.println(motor1.hashCode());
Motor motor2=context.geBean("Motor",Motor.class);
System.out.println(motor2.hashCode());
}
}
when we call context.getBean(...)method then object will be created for proto type
beans
for prototype beans every time new object will be created.
to save memory spring framework made the default scope as singleton
class-14
---------
autowiring
-------------
in application several classes will be available
one class to talk with another class
we are using IOC container to perform that dependancy injection
we are giving instruction to IOC to inject dependant object into target object
using "ref" attribute.
<constructor-arg/>tag like below
<constructor-arg name="payment ref="Upi"/>
</bean>
using"ref" attribute we are telling to IOC which object it has to inject.
this process is called as mannual wiring
spring IOC supports autowiring concept also that means spring IOC having capability
to identify the dependant and inject dependent into target
autowiring having mode
1.by name
2.by type
3.constructor
4. no
file---->new ------>maven project give project name[Auto-Wiring-App]
click finish
now configure path
now in pom.xml add dependencies in our application
mouse right click on project name[Auto-Wiring-App] take new--->class[DiesilEngine]
in that class write the following code
package com.orbit.beans;
public class DiesilEngine
{
public DiesilEngine()
{
System.out.println("DiesilEngine::Constructor");
}
public int start()
{
System.out.println("DiesilEngine Starting ........");
return 1;
}
}
clik on packege name take new class[car.java] write the following code
package com.orbit.beans;
public class car
{
public car()
{
System.out.println("Car ::constructor");
}
}
now we want to represent above 2 classes are spring bean
now right clik on src/main/java take new ------->choose bean configuration file
in spring-Beans xml file write the followng code
<bean id="dieseleng" class=com.orbit.beans.DiesilEngine"/>
<bean id="car" class=com.orbit.beans.Car"/>
in src/main/java take one class to test our application[driver.java]
write the following code
package com.orbit.main;
public class Driver
{
public static void main(String []args)
{
//now we will start the IOC container
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Beans.xml");