SlideShare a Scribd company logo
Reach me @ :
Mallikarjuna G D
gdmallikarjuna@gmail.com
SNIPE TECH PVT LTD BANGALORE
 INVERSION OF CONTROL (IOC) AND DEPENDENCY INJECTION
 ADVANTAGES OF SPRING FRAMEWORK
 SPRING MODULES
 IOC CONTAINER
 SPRING AOP
 SPRING JDBCTEMPLATE
 SPRING WITH ORM FRAMEWORKS
 HIBERNATE AND SPRING INTEGRATION
 SPRING MVC
2
CONTENTS
4/6/2022
3
4/6/2022
INTRODUCTION
• Software
• Programming Language
• Library
• Technology
• Platform
• Framework
4
4/6/2022
INTRODUCTION
•
Reference: https://www.decipherzone.com/blog-detail/top-web-frameworks
5
4/6/2022
INTRODUCTION
•
Reference: https://www.decipherzone.com/blog-detail/top-web-frameworks
6
4/6/2022
INTRODUCTION
• Frameworks are has many inbuilt objects and this will be utilized and our own
objects solve the problems of realtime
• Frameworks usually developed by organization or experience software
professional to reduce the complexity of addressing the business automation
support packages
• The key advantages of framework are efficient, structured and secured
• The key challenges are binded framework guidelines and support dependency
• Examples :Angular, Reactjs, Django, Struts , Spring
Spring Framework
It was developed by Rod Johnson in 2003
Spring framework makes the easy development of JavaEE application.
Spring is a lightweight framework.
It can be thought of as a framework of frameworks because it provides
support to various frameworks
7
INTRODUCTION
4/6/2022
• Lightweight
• Flexible
• Loose coupling
• Declarative support
• Portable
• Well addressed cross cutting concerns
• Well structured configurations
• Life cycles
• Fast
• Secure
• Productivity
8
4/6/2022
9
SPRING FRAMEWORK
4/6/2022
Spring Core Container: The Spring Core container contains core, beans, context and expression
language (EL) modules. It is base module of spring (Bean factory and Application context)
AOP, Aspects and Instrumentation: The aspects module provides support to integration with
AspectJ. Enables cross cutting concerns
Core and Beans: These modules provide IOC and Dependency Injection features to mange life
cycle of java objects
Context: This module supports internationalization (I18N), EJB, JMS, Basic Remoting.
Expression Language: It provides support to setting and getting property values, method
invocation, accessing collections and indexers, named variables, logical and arithmetic operators,
retrieval of objects by name etc.
Test : This layer provides support of testing with JUnit and TestNG.
10
SPRING MODULES
4/6/2022
 Data Access / Integration: This group comprises of JDBC, ORM, OXM, JMS and Transaction
modules. These modules basically provide support to interact with the database.
 Web: This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These modules
provide support to create web application.
 Transaction management: it standardized several transaction API’S to coordinates the
transactions over java objects
 Messaging: Message queues or Java Messaging services and standardizes of message sender over
standard JMS API’s
11
SPRING MODULES
4/6/2022
package com.snipe.learning.springcore;
public class Employee {
public void displayInfo() {
System.out.println("Employee Information Display");
}
}
package com.snipe.learning.springcore;
public class Student {
public void displayInfo() {
System.out.println("Student Information Display");
}
}
12
TRADITIONAL
4/6/2022
package com.snipe.learning.springcore.test;
import com.snipe.learning.springcore.Employee;
import com.snipe.learning.springcore.Student;
public class TestChallengeDI {
public static void main(String args[]) {
Employee employee = new Employee();
employee.displayInfo();
Student student = new Student();
student.displayInfo();
}
}
package com.snipe.learning.springcore;
package com.snipe.learning.springcore;
public interface IPerson {
public void displayInfo();
}
public class Employee {
public void displayInfo() {
System.out.println("Employee Information Display");
}
}
package com.snipe.learning.springcore;
public class Student {
public void displayInfo() {
System.out.println("Student Information Display");
}
}
13
TRADITIONAL-POLYMORPHISM
4/6/2022
package com.snipe.learning.springcore.test;
import com.snipe.learning.springcore.Employee;
import com.snipe.learning.springcore.IPerson;
import com.snipe.learning.springcore.Student;
public class TestChallengeDI {
public static void main(String args[]) {
//polymorphism improvisation
IPerson person = new Employee();
person.displayInfo();
person = new Student();
person.displayInfo();
}
}
package com.snipe.learning.springcore;
public interface IPerson {
public void displayInfo();
}
package com.snipe.learning.springcore;
public class Employee implements IPerson{
public void displayInfo() {
System.out.println("Employee Information Display");
}
}
package com.snipe.learning.springcore;
public class Student implements IPerson{
public void displayInfo() {
System.out.println("Student Information Display");
}
}
14
RETHINK IMPEMENTION
4/6/2022
package com.snipe.learning.springcore;
public class Display {
IPerson person;
public IPerson getPerson() {
return person;
}
public void setPerson(IPerson person) {
this.person = person;
}
public void displayInfo() {
this.person.displayInfo();
}
}
package com.snipe.learning.springcore.test;
import com.snipe.learning.springcore.Display;
import com.snipe.learning.springcore.Employee;
import com.snipe.learning.springcore.Student;
public class TestChallengeDI {
public static void main(String args[]) {
//External supply of objects on needed
//IOC should be developed inject object
// Delegate to the container to do this task
Display display = new Display(); //DELEGATE
display.setPerson(new Student()); //DELEGATE
display.displayInfo();
display = new Display();//DELEGATE
display.setPerson(new Employee());//DELEGATE
display.displayInfo();
}
}
15
DELEGATE OBJECT INIALIZATION ?
4/6/2022
• Tell spring and delegate you inject Display to
Employee and student
• Spring IOC should take care of appropriate
object initialization
• Spring should take care of POJO object
initialization
• Behaviour handled by our Java code should
directly invoke dispaly
16
SPRING SETUP?
4/6/2022
• Download spring latest release distribution https://repo.spring.io/ui/native/release/org/springframework/spring/
• Add jars as library to the project
object
object
object
object
object
object
object
CONTAINER
object
object
object
JAVA BUSINESS OBJECTS
17
SPRING OBJECT FACTORY?
4/6/2022
object
object
object
JAVA BUSINESS OBJECTS
CONFIGURATION
OOBJECT FACTORY
object
18
IOC CONTAINER
4/6/2022
 The IoC container is responsible to instantiate, configure and assemble
the objects. The IoC container gets informations from the XML file and
works accordingly.
 The main tasks performed by IoC container are:
o to instantiate the application class
o to configure the object
o to assemble the dependencies between
the objects.
 There are two types of IoC containers. They are:
1)BeanFactory 2)ApplicationContext
19
IOC CONTAINER
4/6/2022
Spring BeanFactory
BeanFactory is core to the Spring framework
– Lightweight container that loads bean
definitions and manages your beans.
– Configured declaratively using an XML file,
or files, that determine how beans can be referenced and wired together.
– Knows how to serve and manage a singleton or prototype defined bean
– Responsible for lifecycle methods.
– Injects dependencies into defined beans when served
20
IOC CONTAINER
4/6/2022
21
Bean Example
<bean id="employee" class="com.snipe.learning.springcore.di.Employee" />
<bean id="student" class="com.snipe.learning.springcore.di.Student" />
The XmlBeanFactory is the implementation class for the BeanFactory interface.
The constructor of XmlBeanFactory class receives the Resource object
so we need to pass the resource object to create the object of BeanFactory.
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
IOC CONTAINER
4/6/2022
22
public class Employee implements IPerson {
private String cname;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public void displayInfo() {
System.out.println("Employee Company is:" + this.cname);
}
}
INJECT PROEPRTIES
4/6/2022
package com.snipe.learning.springcore.di;
public class Student implements IPerson{
private String cname;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public void displayInfo() {
System.out.println("Student college"+this.cname);
}
}
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee">
<property name="cname" value="SNIPE TECH PVT LTD"></property>
</bean>
<bean id="student"
class="com.snipe.learning.springcore.di.Student">
<property name="cname" value="J.N.N.C.E"></property>
</bean>
23
public class Employee {
private String cname;
private String design;
private float salary;
public Employee(String cname,String design, float salary) {
this.cname = cname;
this.design = design;
this.salary = salary;
}
public void displayInfo() {
System.out.println("Employee Company is:" + this.cname);
System.out.println("Designation is:" + this.design);
System.out.println("Salary is:" + this.salary);
}
}
CONSTRUCTOR INJECTION
4/6/2022
package com.snipe.learning.springcore.di;
public class Student implements IPerson {
private String cname;
private String std;
private float Scholorship;
public Student(String cname, String std, float scholorship) {
this.cname = cname;
this.std = std;
this.Scholorship = scholorship;
}
public void displayInfo() {
System.out.println("College is:" + this.cname);
System.out.println("Standard is:" + this.std);
System.out.println("Scholorship is:" + this.Scholorship);
}
}
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee">
<constructor-arg name="cname" value="SNIPE TECH PVT LTD" />
<constructor-arg name="design" value="SOFTWARE ENGINEER" />
<constructor-arg name="salary" value="30000" />
</bean>
<bean id="student"
class="com.snipe.learning.springcore.di.Student">
<constructor-arg index="0" value="J.N.N.C.E" />
<constructor-arg index="1" value="MTech" />
<constructor-arg index="2" value="10000" />
</bean>
24
<!-- bean definitions here -->
<bean id="employee“ class="com.snipe.learning.springcore.di.Employee">
<property name="cname" value="SNIPE TECH PVT LTD" />
<property name="design" value="SOFTWARE ENGINEER" />
<property name="salary" value="30000" />
<property name="address" ref="empAddress"></property>
</bean>
<bean id="student“ class="com.snipe.learning.springcore.di.Student">
<property name="cname" value="J.N.N.C.E" />
<property name="std" value="MTech" />
<property name="scholorship" value="10000" />
<property name="address" ref="stdAddress"></property>
</bean>
<bean id="empAddress“ class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
<bean id="stdAddress“ class="com.snipe.learning.springcore.di.Address">
<property name="street" value="NT Road" />
<property name="area" value="NAVULE" />
<property name="city" value="SHIMOGA" />
</bean>
INJECT OBJECT
4/6/2022
public class Address {
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
private String street;
private String area;
private String city;
}
25
<!-- bean definitions here -->
<bean id="employee“ class="com.snipe.learning.springcore.di.Employee">
<property name="cname" value="SNIPE TECH PVT LTD" />
<property name="design" value="SOFTWARE ENGINEER" />
<property name="salary" value="30000" />
<property name="address" ref="empAddress"></property>
</bean>
<bean id="student“ class="com.snipe.learning.springcore.di.Student">
<property name="cname" value="J.N.N.C.E" />
<property name="std" value="MTech" />
<property name="scholorship" value="10000" />
<property name="address" ref="stdAddress"></property>
</bean>
<bean id="empAddress“ class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
<bean id="stdAddress“ class="com.snipe.learning.springcore.di.Address">
<property name="street" value="NT Road" />
<property name="area" value="NAVULE" />
<property name="city" value="SHIMOGA" />
</bean>
INJECT OBJECT
4/6/2022
public class Address {
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
private String street;
private String area;
private String city;
}
26
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee">
<property name="cname" value="SNIPE TECH PVT LTD" />
<property name="design" value="SOFTWARE ENGINEER" />
<property name="salary" value="30000" />
<property name="address">
<bean class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
</property>
</bean>
INNER BEAN, ALIAS
4/6/2022
<bean id="student"
class="com.snipe.learning.springcore.di.Student" name="std_details">
<property name="cname" value="J.N.N.C.E" />
<property name="std" value="MTech" />
<property name="scholorship" value="10000" />
<property name="address" ref="stdAddress" />
</bean>
<bean id="empAddress"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
<bean id="stdAddress"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="NT Road" />
<property name="area" value="NAVULE" />
<property name="city" value="SHIMOGA" />
</bean>
<alias name="employee" alias="emp_details"></alias>
</beans>
27
package com.snipe.learning.springcore.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.snipe.learning.springcore.di.Employee;
import com.snipe.learning.springcore.di.Student;
public class TestDIMAIN {
public static void main(String[] args) {
System.out.println("In CLASSPATH BEAN FACTORY");
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
Employee employee = (Employee) factory.getBean("emp_details");
employee.displayInfo();
Student student = (Student) factory.getBean("std_details");
student.displayInfo();
}
}
INNER BEAN, ALIAS
4/6/2022
28
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee">
<property name="addresses">
<list>
<ref bean="homeAddress" />
<ref bean="officeAddress" />
</list>
</property>
</bean>
<bean id="homeAddress"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
<bean id="officeAddress"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="NT Road" />
<property name="area" value="NAVULE" />
<property name="city" value="SHIMOGA" />
</bean>
<alias name="employee" alias="emp_details"></alias>
</beans>
INJECTING COLLECTIONS
4/6/2022
package com.snipe.learning.springcore.di;
import java.util.List;
public class Employee implements IPerson {
private List<Address> addresses;
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public void displayInfo() {
for (Address address : addresses) {
System.out
.println("Address is:" + address.getStreet() + "::" + address.getArea() +
"::" + address.getCity());
}
}
}
29
autowire
4/6/2022
Autowiring is injecting object implicitly without external implementation. This
works matching by Name, Type or constructor arguments.
This works only for reference objects not for primitive or strings
Mode Description
no No autowiring default
byName The byName mode injects the based on the name. In this case property name and bean name must be same. It
internally calls setter method.
byType The byType mode injects based on type object. So property name and bean name can be different. It internally calls
setter method. It works only one bean exists.
constructor The constructor mode injects by calling the constructor of the class. It calls the constructor having large number of
parameters. It works only one bean of that class.
30
AUTOWIRED BY NAME
4/6/2022
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee"
autowire="byName"></bean>
<bean id="home_addresses"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga"
/>
<property name="area" value="srirampura 2nd
stage" />
<property name="city" value="mysuru" />
</bean>
<bean id="office_addresses"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="NT Road" />
<property name="area" value="NAVULE" />
<property name="city" value="SHIMOGA" />
</bean>
</beans>
package com.snipe.learning.springcore.di;
public class Employee implements IPerson {
private Address home_addresses;
public Address getHome_addresses() {
return home_addresses; }
public void setHome_addresses(Address
home_addresses){
this.home_addresses = home_addresses;
}
public Address getOffice_addresses() {
return office_addresses;
}
public void setOffice_addresses(Address
office_addresses) {
this.office_addresses = office_addresses; }
private Address office_addresses;
public void displayInfo() {
System.out.println("Employee Details");
System.out.println("Home
address::"+this.home_addresses.getStreet()+"
::"+this.home_addresses.getArea()+"
::"+this.home_addresses.getCity());
System.out.println("Office
address::"+this.office_addresses.getStreet()+"
::"+this.office_addresses.getArea()+"
::"+this.office_addresses.getCity());
}
}
31
AUTOWIRED BY TYPE
4/6/2022
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee"
autowire="byType"></bean>
<bean id="homeAddress"
class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
</beans>
package com.snipe.learning.springcore.di;
public class Employee implements IPerson {
private Address home_addresses;
public Address getHome_addresses() {
return home_addresses;
}
public void setHome_addresses(Address
home_addresses) {
this.home_addresses = home_addresses;
}
public void displayInfo() {
System.out.println("Employee Details");
System.out.println("Home
address::"+this.home_addresses.getStreet()+"
::"+this.home_addresses.getArea()+"
::"+this.home_addresses.getCity());
}
}
}
32
AUTOWIRED BY CONSTRUCTOR
4/6/2022
<!-- bean definitions here -->
<bean id="employee"
class="com.snipe.learning.springcore.di.Employee"
autowire="constructor">
<constructor-arg name="cname" value="SNIPE TECH PVT LTD" />
<constructor-arg name="design" value="SOFTWARE ENGINEER" />
<constructor-arg name="salary" value="30000" />
</bean>
</beans>
package com.snipe.learning.springcore.di;
public class Employee implements IPerson {
private String cname;
private String design;
private float salary;
public Employee(String cname, String design, float salary)
{
this.cname = cname;
this.design = design;
this.salary = salary;
}
public void displayInfo() {
System.out.println("Employee Details::" +
this.cname + " ::" + this.design + " ::" + this.salary);
}
}
}
33
scope
4/6/2022
The Spring scopes are tells how many instances of beans are available to use it.
By default only one bean instance will be created as and when context file loaded.
Scopes are
• Singleton – only one instance created during loading of context xml flile
• Prototype –it creates more than one instance as and when the getBean method
called. To each request the instance will be created.
• Web aware context in spring
• Request –spring on each servlet request bean will be created
• Session –New bean per session
• Global –session –New bean per one HTTP Request session(portlet context)
<bean id="employee" class="com.snipe.learning.springcore.di.Employee" autowire="byType" scope="prototype"></bean>
<bean id="homeAddress" class="com.snipe.learning.springcore.di.Address">
<property name="street" value="shatrugna marga" />
<property name="area" value="srirampura 2nd stage" />
<property name="city" value="mysuru" />
</bean>
ANNOTATION
@Bean
@Scope("prototype")
34
ApplicationContextAware, BeanNameAware
4/6/2022
• BeanFactory instantiate bean when you call getBean() method
while ApplicationContext instantiate Singleton bean when container is started,
It doesn't wait for getBean() to be called.
• BeanFactory doesn't provide support for internationalization
but ApplicationContext provides support for it.
• Another difference between BeanFactory vs ApplicationContext is ability to
publish event to beans that are registered as listener.
• One of the popular implementation of BeanFactory interface
is XMLBeanFactory while one of the popular implementation
of ApplicationContext interface is ClassPathXmlApplicationContext.
• If you are using auto wiring and using BeanFactory than you need to
register AutoWiredBeanPostProcessor using API which you can configure in
XML if you are using ApplicationContext. In summary BeanFactory is OK for
testing and non production use but ApplicationContext is more feature rich
container implementation and should be favored over BeanFactory
35
ApplicationContextAware, BeanNameAware
4/6/2022
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Student implements ApplicationContextAware, BeanNameAware {
private ApplicationContext context = null;
public ApplicationContext getContext() {
return context;
}
@Override
public void setBeanName(String bean) {
// TODO Auto-generated method stub
System.out.println("bean name::" + bean);
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
// TODO Auto-generated method stub
this.context = context;
}
}
36
INHERITANCE
4/6/2022
public class Person {
protected String name;
protected int age;
protected String sex;
..
}
public class Student extends Person {
private String cname;
private String std;
private float Scholorship;
..
}
<bean id="person" class="com.snipe.learning.springcore.di.Person">
<property name="name" value="Pranet M D" />
<property name="age" value="13" />
<property name="sex" value="Male" />
</bean>
<bean id="student"
class="com.snipe.learning.springcore.di.Student" parent="person">
<property name="cname" value="J.N.N.C.E" />
<property name="std" value="MTech" />
<property name="scholorship" value="10000" />
</bean>
37
LIFE CYCLE CALL BACK METHODS
4/6/2022
public class Student extends Person implements InitializingBean, DisposableBean {
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("Destry bean"); }
public void afterPropertiesSet() throws Exception {
System.out.println("Initializing bean"); }
public void init_bean() {
System.out.println("Init method");
}
public void cleanup() {
System.out.println("cleanup method"); }
}
<bean id="person" class="com.snipe.learning.springcore.di.Person">
<property name="name" value="Pranet M D" />
<property name="age" value="13" />
<property name="sex" value="Male" />
</bean>
<bean id="student"
class="com.snipe.learning.springcore.di.Student" parent="person" init-method="init_bean" destroy-method="cleanup">
<property name="cname" value="J.N.N.C.E" />
<property name="std" value="MTech" />
<property name="scholorship" value="10000" />
</bean>
38
INTERFACE USAGE
4/6/2022
package com.snipe.learning.springcore.di;
public interface IPerson {
public void displayInfo();
}
package com.snipe.learning.springcore.di;
public class Student implements IPerson {
@Override
public void displayInfo() {
System.out.println("STUDENT INFORMATION");
} }
package com.snipe.learning.springcore.di;
public class Employee implements IPerson{
@Override
public void displayInfo() {
System.out.println("EMPLOYEE INFORMATION");
}
}
<!-- bean definitions here -->
<!-- <bean id="person" class="com.snipe.learning.springcore.di.IPerson"/>
--><bean id="student" class="com.snipe.learning.springcore.di.Student" />
<bean id="employee" class="com.snipe.learning.springcore.di.Employee" />
</beans>
public class TestDIMAIN {
public static void main(String[] args) {
System.out.println("In CLASSPATH BEAN FACTORY");
AbstractApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
context.registerShutdownHook();
IPerson student = (Student)context.getBean("student");
student.displayInfo();
IPerson employee = (Employee)context.getBean("employee");
employee.displayInfo();
}
}
39
SPRING AOP
4/6/2022
• Aspect-Oriented Programming (AOP)
complements Object-Oriented
Programming (OOP) by providing another
way of thinking about program structure.
• Aspects enable the modularization of
concerns such as transaction management
that cut across multiple types and objects.
(Such concerns are often
termed crosscutting concerns in AOP
literature.)
• Objects have other than business model
details like managing logging, transaction,
security so on
• This code required in all methods
• We cannot manage or modify at once.
Where use AOP
 AOP is mostly used in following cases:
• to provide declarative enterprise services such
as declarative transaction management.
• It allows users to implement
custom aspects.
Steps of AOP
• Write Aspect
• Configure Aspect
40
SPRING AOP
4/6/2022
hierarchy of advice interfaces
41
SPRING AOP
4/6/2022
Core AOP concepts
 Join point
An identifiable point in the execution of a program.
Central, distinguishing concept in AOP
 Pointcut
Program construct that selects join points and collects context at
those points.
 Advice
Code to be executed at a join point that has been selected by a
pointcut
 Introduction
Additional data or method to existing types, implementing new
interfaces
42
SPRING AOP
4/6/2022
• Advice
• Advice represents an action taken by an aspect at a particular join point.
There are different types of advices:
• Before Advice: it executes before a join point.
• After Returning Advice: it executes after a joint point completes
normally.
• After Throwing Advice: it executes if method exits by throwing an
exception.
• After (finally) Advice: it executes after a join point regardless of join
point exit whether normally or exceptional return.
• Around Advice: It executes before and after a join point.
43
SPRING AOP
4/6/2022
Spring AOP AspectJ Annotation
• There are two ways to use Spring AOP AspectJ implementation:
• By annotation: We are going to learn it here.
• By xml configuration
(schema based)
44
SPRING AOP
4/6/2022
• Spring AspectJ AOP implementation provides many annotations:
• @Aspect
• @Pointcut:
The annotations used to create advices are given below:
• @Before
• @After
• @After Returning
• @Around
• @AfterThrowing
45
SPRING AOP
4/6/2022
Bean class
public class ExampleModel {
public void read() {
System.out.println("read info");
}
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.ExampleBeforeA
spect"/>
46
@BEFORE
4/6/2022
@Aspect
public class ExampleBeforeAspect {
@Before("allread()")
public void LoggingAdvice() {
System.out.println("Logging Advice");
}
@Pointcut("execution(public void read())")
public void allread() {}
}
Output:
Logging Advice
read info
Bean class
public class ExampleModel {
public void read() {
System.out.println("read info");
}
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.ExampleAfterAs
pect"/>
47
@AFTER
4/6/2022
@Aspect
public class ExampleAfterAspect {
@After("allread()")
public void LoggingAdvice() {
System.out.println("Logging Advice");
}
@Pointcut("execution(public void read())")
public void allread() {}
}
Output:
read info
Logging Advice
Bean class
public class ExampleModel {
public void read() {
System.out.println("read info");
//throw new RuntimeException();
}
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.ExampleAfterAs
pect"/>
48
@AFTERRETURNING
4/6/2022
@Aspect
public class ExampleAfterAspect {
@AfterReturning("allread()")
public void LoggingAdvice() {
System.out.println("Logging Advice");
}
@Pointcut("execution(public void read())")
public void allread() {}
}
Output:
read info
Logging Advice
Bean class
public class ExampleModel {
public void read() {
System.out.println("read info");
int nume = 10/0;
System.out.println(num); }
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.ExampleExceptio
nAspect"/>
49
@AFTERTHROWING
4/6/2022
@Aspect
public class ExampleExceptionAspect {
@AfterReturning("allread()")
public void ReturningAdvice() {
System.out.println("Returning Advice");
}
@AfterThrowing("allread()")
public void ExceptionAdvice() {
System.out.println("Exception Advice");
}
@Pointcut("execution(public void read())")
public void allread() {}
}
Output:
read info
Exception Advice
Exception in thread "main" java.lang.ArithmeticException: / by zero
Bean class
package com.snipe.learning.aop.model;
public class ExampleModel {
public String readInfo(String name) {
System.out.println("read info");
System.out.println(name);
return name;
}
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.
ExampleAfteReturningAspect"/>
50
@AFTERRETURNING
4/6/2022
@@Aspect
public class ExampleAfteReturningAspect {
@AfterReturning("args(name)")
public void LoggingAdvice(String name) {
System.out.println("Logging Advice"+name);
}
@Pointcut("execution(public void read())")
public void allread() {}
}
Output:
read info
mallikarjuna
Logging Advicemallikarjuna
Bean class
package com.snipe.learning.aop.model;
public class ExampleModel {
public void readInfo() {
System.out.println("read info");
}
}
applicationContext.xml
<aop:aspectj-autoproxy/>
<bean id="exampleModel"
class="com.snipe.learning.aop.model.ExampleModel" />
<bean id="aspectWorkExample"
class="com.snipe.learning.aop.aspectservice.ExampleAroundA
spect"/>
51
@AROUND
4/6/2022
@Around("allread()")
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("Logging Advice");
try {
System.out.println("before advice");
proceedingJoinPoint.proceed();
System.out.println("after advice");
}catch(Throwable te) {
System.out.println("After Throwing");
}
}
@Pointcut("execution(public void readInfo())")
public void allread() {}
Test ::
ApplicationContext context = new ClassPathXmlApplicationContext
("applicationContext.xml");
ExampleModel exampleModel =context.getBean
("exampleModel", ExampleModel.class);
exampleModel.readInfo();
Output:
Logging Advice
before advice
read info
after advice
52
SPRING AOP
4/6/2022
@Aspect
public class AspectWork {
@Before("execution(public void readInfo())")
public void LoggingAdvice() {
System.out.println("Logging Advice");
}
@Before("execution(public void printInfo())")
public void PrintAdvice() {
System.out.println("Print Advice");
}
@Before("execution(* get*(..))")
public void ReadAdvice() {
System.out.println("Read advice");
}
@After("execution(* get*(..))")
public void WriteAdvice() {
System.out.println("Write advice");
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<aop:aspectj-autoproxy/>
<bean id="student" class="com.snipe.learning.aop.model.Student">
<property name="name" value="Pranet M D" />
</bean>
<bean id="employee" class="com.snipe.learning.aop.model.Employee">
<property name="name" value="Mallikarjuna G D" />
</bean>
<bean id="person" class="com.snipe.learning.aop.model.Person"
autowire="byName" />
<bean id="aspectWork"
class="com.snipe.learning.aop.aspectservice.AspectWork"/>
</beans>
Data Access Model:
Spring JdbcTemplate
Problems of JDBC API
The problems of JDBC API are as follows:
 We need to write a lot of code before and after executing the query,
such as creating connection, statement, closing resultset, connection etc.
 We need to perform exception handling code on the database logic.
 Repetition of all these codes from one to another database logic is a
time consuming task.
53
Spring JdbcTemplate
4/6/2022
Advantage of Spring JdbcTemplate
• Spring JdbcTemplate eliminates all the above mentioned
problems of JDBC API. It provides you methods to write
the queries directly, so it saves a lot of
work and time.
54
Spring JdbcTemplate
4/6/2022
JdbcTemplate class
 methods of spring JdbcTemplate class
55
Spring JdbcTemplate
4/6/2022
 Spring Jdbc Approaches
Spring framework provides following approaches for JDBC database
access:
 JdbcTemplate
 NamedParameterJdbcTemplate
 SimpleJdbcTemplate
 SimpleJdbcInsert and SimpleJdbcCall
56
Spring JdbcTemplate
4/6/2022
Spring MVC
• In Spring Web MVC, DispatcherServlet class works as the front controller. It
is responsible to manage the flow of the spring mvc application.
4/6/2022 57
Spring MVC
Advantages of Spring 3.0 MVC
 Supports RESTful URLs.
 Annotation based configuration.
 Supports to plug with other MVC frameworks like Struts etc.
 Flexible in supporting different view types like JSP, velocity, XML,
PDF etc.,
4/6/2022 58
Spring MVC
Front Controller - Responsiblities
• Initialize the framework to cater to the requests.
• Load the map of all the URLs and the components
responsible to handle the request.
• Prepare the map for the views.
4/6/2022 59
Spring MVC
Spring Boot
 Spring Boot File Structure
 Spring Boot Simple Example
 SPRING BOOT IN ECLIPSE
4/6/2022
61
CONTENTS
• Spring Boot is a Framework from “The Spring Team” to ease the
bootstrapping and development of new Spring Applications.
4/6/2022 62
INTRODUCTION
• Spring Boot makes it easy to create stand-alone, production-grade
Spring based Applications that you can “just run”.
• We take an opinionated view of the Spring platform and third-party
libraries so you can get started with minimum fuss.
• Most Spring Boot applications need very little Spring configuration.
 INTRODUCTION
What is Spring ?
4/6/2022 63
Application framework
Programming and Configuration model
Infrastructure support
Challenges
Looks like very vast framework
Lot more setup and configuration hudles
Lot more deployment modes
What is Spring Boot ?
• It is a Spring module which provides RAD (Rapid Application Development)
feature to Spring framework.
• It provides defaults for code and annotation configuration to quick start new
Spring projects within no time. It follows “Opinionated Defaults
Configuration” Approach to avoid lot of boilerplate code and configuration to
improve Development, Unit Test and Integration Test Process.
4/6/2022 64
SPRING BOOT
SPRING
FRAMEWORK
Embedded HHTP
Servers,
(Tomcat, Jetty)
XML <Bean>
Configuration
WHY USE SPRING BOOT?
• To ease the Java-based applications Development, Unit Test and
Integration Test Process and Time. To increase Productivity.
• To avoid XML Configuration completely.
• To avoid defining more Annotation Configuration(It combined some
existing Spring Framework Annotations to a simple and single
Annotation)
• To avoid writing lots of import statements
• To provide some defaults to quick start new projects within no time.
• To provide Opinionated Development approach.
4/6/2022 65
WHY USE SPRING BOOT?
• To Setup default configuration
• Starts a application context
• Problems of class path scan
• Starts tomcat server
4/6/2022 66
WHAT SPRING BOOT DOES
Spring
• Dependency Injection Framework
• Manage Lifecycle Of Java(Beans)
• Boiler plate configuration
-programmer writes a lot of code
to minimal task
• Takes Time to have a spring
application up and run
Spring Boot
• A suite of pre-configured
frameworks and technologies
-That is used to remove
boilerplate configuration
• The shortest way to have
application up and running
4/6/2022 67
Spring Boot Framework has mainly four major Components.
 Spring Boot Starters: Spring Boot Starters is one of the major key features or
components of Spring Boot Framework. The main responsibility of Spring Boot Starter is to
combine a group of common or related dependencies into single dependencies.
 Spring Boot Auto Configurator; AutoConfigurator is to reduce the Spring Configuration. If we
develop Spring applications in Spring Boot, then We don't need to define single XML configuration and
almost no or minimal Annotation configuration. Spring Boot Auto Configurator component will take care of
providing those information.
 Spring Boot CLI: Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test
Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it
internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all
dependencies and execute the application.
4/6/2022 68
COMPONENTS OF SPRING BOOT
 Spring Boot Starters: The spring-boot-actuator module provides all of Spring Boot’s production-
ready features. The recommended way to enable the features is to add a dependency on the spring-boot-
starter-actuator “Starter”.
4/6/2022 69
COMPONENTS OF SPRING BOOT
CLI
Actuator Starters
Auto
Configurator
SPRINGBOOT
COMPONENTS
4/6/2022 70
COMPONENTS OF SPRING BOOT
COMPONENTS OF SPRING BOOT
Its look like:
4/6/2022 71
EXAMPLES
• Spring Boot Starters:
example: if we want to get started using Spring and JPA for database access,
just include the spring-boot-starter-data-jpa dependency
Pattern: spring-boot-starter-*, where *
spring-boot-starter-web It is used for building web, including RESTful, applications
using Spring MVC. Uses Tomcat as the default embedded
container.
spring-boot-starter-jdbc It is used for JDBC with the Tomcat JDBC
connection pool.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
COMPONENTS OF SPRING BOOT
4/6/2022 72
• Spring Boot Auto Configurator
example; • Spring Boot Starter reduces build’s dependencies and Spring Boot
AutoConfigurator reduces the Spring Configuration.
• Spring Boot Starter has a dependency on Spring Boot
AutoConfigurator, Spring Boot Starter triggers Spring Boot
AutoConfigurator automatically.
@SpringBootAppliction @Configuration @ComponentScan @EnableAutoConfig
COMPONENTS OF SPRING BOOT
 Spring Boot Starters: The spring-boot-actuator module provides all of Spring Boot’s production-
ready features. The recommended way to enable the features is to add a dependency on the spring-boot-
starter-actuator “Starter”.
4/6/2022 73
COMPONENTS OF SPRING BOOT
4/6/2022 74
SPRING BOOT STRUCTURE
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.snipe.learning.springboot</groupId>
<artifactId>SpringBootApp</artifactId>
<version>0.0.1-SNAPSHOT</version><name>my first learn</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<properties><project.build.sourceEncoding>UTF-
8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target></properties>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
</dependencies>
<build>
<plugins> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
. Bill of Materials
version dependency management of all the dependent child jars
• Embedded Tomcat Server
– Manage the servlet container
– Standalone run
– Microservice architecture support
– Developer free from lot of configurations
4/6/2022 75
HIDDEN TREASURE
 Post Man
4/6/2022 76
We will use this postman application for using
these services
• GET
• POST
• PUT
• DELETE
SPRING BOOT IN ECLIPSE
4/6/2022 77
The spring boot can be booted through: Spring Boot CLI
SPRING BOOT CLI
4/6/2022 78
https://spring.io/tools
•
SPRING TOOL SUIT(STS)
4/6/2022 79
• See the website for common application.properites
spring application.properties
1) What is Spring?
2) What are the advantages of spring framework?
3) What are the modules of spring framework?
4) What is IOC and DI?
5) What is the role of IOC container in spring?
6) What are the types of IOC container in spring?
7) What is the difference between BeanFactory and ApplicationContext?
8) What is the difference between constructor injection and setter injection?
9) What is autowiring in spring? What are the autowiring modes?
10) What are the different bean scopes in spring?
11) In which scenario, you will use singleton and prototype scope?
80
Spring Interview Questions
12 What are the advantages of JdbcTemplate in spring?
13) What are the advantages of spring AOP?
14) What is interceptor?
15)Does spring perform weaving at compile time?
16) What are the AOP implementation?
17) What is the front controller class of Spring MVC?
18) What does @Controller annotation?
19) What does @RequestMapping annotation?
20) What does the ViewResolver class?
21) Which ViewResolver class is widely used?
22) Does spring MVC provide validation support? 81
Spring Interview Questions
https://docs.spring.io/spring-framework/docs/current/reference/html/
https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html
https://www.javatpoint.com/
https://www.tutorialspoint.com/java/
https://www.w3schools.com/java/
82
Spring References
THANK YOU
83
4/6/2022

More Related Content

PPT
Maven Introduction
Sandeep Chawla
 
PPTX
Angular 2.0
Mallikarjuna G D
 
PPTX
Welcome to Blazor
dark_wisdom
 
PPTX
Spring @Transactional Explained
Victor Rentea
 
PDF
版本控制 使用Git & git hub
維佋 唐
 
PPT
Rhapsody Software
Bill Duncan
 
PDF
Spring Boot
koppenolski
 
Maven Introduction
Sandeep Chawla
 
Angular 2.0
Mallikarjuna G D
 
Welcome to Blazor
dark_wisdom
 
Spring @Transactional Explained
Victor Rentea
 
版本控制 使用Git & git hub
維佋 唐
 
Rhapsody Software
Bill Duncan
 
Spring Boot
koppenolski
 

What's hot (20)

PPTX
Code smells and remedies
Md.Mojibul Hoque
 
PDF
Testing with JUnit 5 and Spring
VMware Tanzu
 
PDF
칸반(Kanban)
영기 김
 
PDF
Introduction to gradle
NexThoughts Technologies
 
PDF
I Love APIs 2015 : Zero to Thousands TPS Private Cloud Operations Workshop
Apigee | Google Cloud
 
PPTX
Software development best practices & coding guidelines
Ankur Goyal
 
PDF
Starting with Git & GitHub
Nicolás Tourné
 
PDF
Virtual Machines and Docker
Danish Khakwani
 
PPTX
Spring boot Introduction
Jeevesh Pandey
 
PDF
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
PDF
Effective CMake
Daniel Pfeifer
 
ODP
Introduction to Swagger
Knoldus Inc.
 
PPTX
CI/CD for React Native
Joao Marins
 
PDF
88 c-programs
Minh Thắng Trần
 
PPTX
Spring Boot and REST API
07.pallav
 
PPTX
Interview preparation full_stack_java
Mallikarjuna G D
 
PDF
Testing Angular
Lilia Sfaxi
 
PPTX
Ansible with Jenkins in a CI/CD Process
Khairul Zebua
 
PDF
Dependency Injection
Giovanni Scerra ☃
 
Code smells and remedies
Md.Mojibul Hoque
 
Testing with JUnit 5 and Spring
VMware Tanzu
 
칸반(Kanban)
영기 김
 
Introduction to gradle
NexThoughts Technologies
 
I Love APIs 2015 : Zero to Thousands TPS Private Cloud Operations Workshop
Apigee | Google Cloud
 
Software development best practices & coding guidelines
Ankur Goyal
 
Starting with Git & GitHub
Nicolás Tourné
 
Virtual Machines and Docker
Danish Khakwani
 
Spring boot Introduction
Jeevesh Pandey
 
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
Effective CMake
Daniel Pfeifer
 
Introduction to Swagger
Knoldus Inc.
 
CI/CD for React Native
Joao Marins
 
88 c-programs
Minh Thắng Trần
 
Spring Boot and REST API
07.pallav
 
Interview preparation full_stack_java
Mallikarjuna G D
 
Testing Angular
Lilia Sfaxi
 
Ansible with Jenkins in a CI/CD Process
Khairul Zebua
 
Dependency Injection
Giovanni Scerra ☃
 
Ad

Similar to Spring andspringboot training (20)

PDF
Spring Framework Tutorial | VirtualNuggets
Virtual Nuggets
 
PDF
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
PPTX
Introduction to Spring Framework
ASG
 
PPTX
Introduction to Spring Framework
Dineesha Suraweera
 
PPTX
Spring (1)
Aneega
 
PDF
Spring mvc
Hamid Ghorbani
 
PPT
Spring ppt
Mumbai Academisc
 
PDF
Spring 2
Aruvi Thottlan
 
PDF
Spring Framework 4.0 to 4.1
Sam Brannen
 
PDF
Getting Started with Spring Framework
Edureka!
 
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
PPTX
Spring tutorials
TIB Academy
 
PPTX
Spring Framework Rohit
Rohit Prabhakar
 
PPTX
unit_1_spring_1.pptxfgfgggjffgggddddgggg
zmulani8
 
PDF
Spring framework Introduction
Anuj Singh Rajput
 
PPTX
1. Spring intro IoC
ASG
 
ODT
Spring framework
Shivi Kashyap
 
PPTX
Spring
Kamalmeet Singh
 
PDF
Spring core module
Raj Tomar
 
PPT
Spring Framework
nomykk
 
Spring Framework Tutorial | VirtualNuggets
Virtual Nuggets
 
Getting Started With Spring Framework J Sharma Ashish Sarin
moineittay
 
Introduction to Spring Framework
ASG
 
Introduction to Spring Framework
Dineesha Suraweera
 
Spring (1)
Aneega
 
Spring mvc
Hamid Ghorbani
 
Spring ppt
Mumbai Academisc
 
Spring 2
Aruvi Thottlan
 
Spring Framework 4.0 to 4.1
Sam Brannen
 
Getting Started with Spring Framework
Edureka!
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Arjun Thakur
 
Spring tutorials
TIB Academy
 
Spring Framework Rohit
Rohit Prabhakar
 
unit_1_spring_1.pptxfgfgggjffgggddddgggg
zmulani8
 
Spring framework Introduction
Anuj Singh Rajput
 
1. Spring intro IoC
ASG
 
Spring framework
Shivi Kashyap
 
Spring core module
Raj Tomar
 
Spring Framework
nomykk
 
Ad

More from Mallikarjuna G D (20)

PPTX
Reactjs
Mallikarjuna G D
 
PPTX
Bootstrap 5 ppt
Mallikarjuna G D
 
PPTX
Hibernate
Mallikarjuna G D
 
PPT
Jspprogramming
Mallikarjuna G D
 
PPT
Servlet programming
Mallikarjuna G D
 
PPT
Servlet programming
Mallikarjuna G D
 
PPTX
Mmg logistics edu-final
Mallikarjuna G D
 
PPTX
Interview preparation net_asp_csharp
Mallikarjuna G D
 
PPTX
Interview preparation devops
Mallikarjuna G D
 
PPTX
Interview preparation testing
Mallikarjuna G D
 
PPTX
Interview preparation data_science
Mallikarjuna G D
 
PPTX
Enterprunership
Mallikarjuna G D
 
PPTX
Core java
Mallikarjuna G D
 
PPTX
Type script
Mallikarjuna G D
 
PPTX
Angularj2.0
Mallikarjuna G D
 
PPTX
Git Overview
Mallikarjuna G D
 
PPTX
Jenkins
Mallikarjuna G D
 
PPT
Hadoop
Mallikarjuna G D
 
PPT
Digital marketing
Mallikarjuna G D
 
Bootstrap 5 ppt
Mallikarjuna G D
 
Hibernate
Mallikarjuna G D
 
Jspprogramming
Mallikarjuna G D
 
Servlet programming
Mallikarjuna G D
 
Servlet programming
Mallikarjuna G D
 
Mmg logistics edu-final
Mallikarjuna G D
 
Interview preparation net_asp_csharp
Mallikarjuna G D
 
Interview preparation devops
Mallikarjuna G D
 
Interview preparation testing
Mallikarjuna G D
 
Interview preparation data_science
Mallikarjuna G D
 
Enterprunership
Mallikarjuna G D
 
Core java
Mallikarjuna G D
 
Type script
Mallikarjuna G D
 
Angularj2.0
Mallikarjuna G D
 
Git Overview
Mallikarjuna G D
 
Digital marketing
Mallikarjuna G D
 

Recently uploaded (20)

PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
FSSAI (Food Safety and Standards Authority of India) & FDA (Food and Drug Adm...
Dr. Paindla Jyothirmai
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Trends in pediatric nursing .pptx
AneetaSharma15
 

Spring andspringboot training

  • 1. Reach me @ : Mallikarjuna G D [email protected] SNIPE TECH PVT LTD BANGALORE
  • 2.  INVERSION OF CONTROL (IOC) AND DEPENDENCY INJECTION  ADVANTAGES OF SPRING FRAMEWORK  SPRING MODULES  IOC CONTAINER  SPRING AOP  SPRING JDBCTEMPLATE  SPRING WITH ORM FRAMEWORKS  HIBERNATE AND SPRING INTEGRATION  SPRING MVC 2 CONTENTS 4/6/2022
  • 3. 3 4/6/2022 INTRODUCTION • Software • Programming Language • Library • Technology • Platform • Framework
  • 6. 6 4/6/2022 INTRODUCTION • Frameworks are has many inbuilt objects and this will be utilized and our own objects solve the problems of realtime • Frameworks usually developed by organization or experience software professional to reduce the complexity of addressing the business automation support packages • The key advantages of framework are efficient, structured and secured • The key challenges are binded framework guidelines and support dependency • Examples :Angular, Reactjs, Django, Struts , Spring
  • 7. Spring Framework It was developed by Rod Johnson in 2003 Spring framework makes the easy development of JavaEE application. Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks 7 INTRODUCTION 4/6/2022
  • 8. • Lightweight • Flexible • Loose coupling • Declarative support • Portable • Well addressed cross cutting concerns • Well structured configurations • Life cycles • Fast • Secure • Productivity 8 4/6/2022
  • 10. Spring Core Container: The Spring Core container contains core, beans, context and expression language (EL) modules. It is base module of spring (Bean factory and Application context) AOP, Aspects and Instrumentation: The aspects module provides support to integration with AspectJ. Enables cross cutting concerns Core and Beans: These modules provide IOC and Dependency Injection features to mange life cycle of java objects Context: This module supports internationalization (I18N), EJB, JMS, Basic Remoting. Expression Language: It provides support to setting and getting property values, method invocation, accessing collections and indexers, named variables, logical and arithmetic operators, retrieval of objects by name etc. Test : This layer provides support of testing with JUnit and TestNG. 10 SPRING MODULES 4/6/2022
  • 11.  Data Access / Integration: This group comprises of JDBC, ORM, OXM, JMS and Transaction modules. These modules basically provide support to interact with the database.  Web: This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet. These modules provide support to create web application.  Transaction management: it standardized several transaction API’S to coordinates the transactions over java objects  Messaging: Message queues or Java Messaging services and standardizes of message sender over standard JMS API’s 11 SPRING MODULES 4/6/2022
  • 12. package com.snipe.learning.springcore; public class Employee { public void displayInfo() { System.out.println("Employee Information Display"); } } package com.snipe.learning.springcore; public class Student { public void displayInfo() { System.out.println("Student Information Display"); } } 12 TRADITIONAL 4/6/2022 package com.snipe.learning.springcore.test; import com.snipe.learning.springcore.Employee; import com.snipe.learning.springcore.Student; public class TestChallengeDI { public static void main(String args[]) { Employee employee = new Employee(); employee.displayInfo(); Student student = new Student(); student.displayInfo(); } }
  • 13. package com.snipe.learning.springcore; package com.snipe.learning.springcore; public interface IPerson { public void displayInfo(); } public class Employee { public void displayInfo() { System.out.println("Employee Information Display"); } } package com.snipe.learning.springcore; public class Student { public void displayInfo() { System.out.println("Student Information Display"); } } 13 TRADITIONAL-POLYMORPHISM 4/6/2022 package com.snipe.learning.springcore.test; import com.snipe.learning.springcore.Employee; import com.snipe.learning.springcore.IPerson; import com.snipe.learning.springcore.Student; public class TestChallengeDI { public static void main(String args[]) { //polymorphism improvisation IPerson person = new Employee(); person.displayInfo(); person = new Student(); person.displayInfo(); } }
  • 14. package com.snipe.learning.springcore; public interface IPerson { public void displayInfo(); } package com.snipe.learning.springcore; public class Employee implements IPerson{ public void displayInfo() { System.out.println("Employee Information Display"); } } package com.snipe.learning.springcore; public class Student implements IPerson{ public void displayInfo() { System.out.println("Student Information Display"); } } 14 RETHINK IMPEMENTION 4/6/2022 package com.snipe.learning.springcore; public class Display { IPerson person; public IPerson getPerson() { return person; } public void setPerson(IPerson person) { this.person = person; } public void displayInfo() { this.person.displayInfo(); } }
  • 15. package com.snipe.learning.springcore.test; import com.snipe.learning.springcore.Display; import com.snipe.learning.springcore.Employee; import com.snipe.learning.springcore.Student; public class TestChallengeDI { public static void main(String args[]) { //External supply of objects on needed //IOC should be developed inject object // Delegate to the container to do this task Display display = new Display(); //DELEGATE display.setPerson(new Student()); //DELEGATE display.displayInfo(); display = new Display();//DELEGATE display.setPerson(new Employee());//DELEGATE display.displayInfo(); } } 15 DELEGATE OBJECT INIALIZATION ? 4/6/2022 • Tell spring and delegate you inject Display to Employee and student • Spring IOC should take care of appropriate object initialization • Spring should take care of POJO object initialization • Behaviour handled by our Java code should directly invoke dispaly
  • 16. 16 SPRING SETUP? 4/6/2022 • Download spring latest release distribution https://repo.spring.io/ui/native/release/org/springframework/spring/ • Add jars as library to the project object object object object object object object CONTAINER object object object JAVA BUSINESS OBJECTS
  • 17. 17 SPRING OBJECT FACTORY? 4/6/2022 object object object JAVA BUSINESS OBJECTS CONFIGURATION OOBJECT FACTORY object
  • 18. 18 IOC CONTAINER 4/6/2022  The IoC container is responsible to instantiate, configure and assemble the objects. The IoC container gets informations from the XML file and works accordingly.  The main tasks performed by IoC container are: o to instantiate the application class o to configure the object o to assemble the dependencies between the objects.
  • 19.  There are two types of IoC containers. They are: 1)BeanFactory 2)ApplicationContext 19 IOC CONTAINER 4/6/2022
  • 20. Spring BeanFactory BeanFactory is core to the Spring framework – Lightweight container that loads bean definitions and manages your beans. – Configured declaratively using an XML file, or files, that determine how beans can be referenced and wired together. – Knows how to serve and manage a singleton or prototype defined bean – Responsible for lifecycle methods. – Injects dependencies into defined beans when served 20 IOC CONTAINER 4/6/2022
  • 21. 21 Bean Example <bean id="employee" class="com.snipe.learning.springcore.di.Employee" /> <bean id="student" class="com.snipe.learning.springcore.di.Student" /> The XmlBeanFactory is the implementation class for the BeanFactory interface. The constructor of XmlBeanFactory class receives the Resource object so we need to pass the resource object to create the object of BeanFactory. Resource resource=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource); IOC CONTAINER 4/6/2022
  • 22. 22 public class Employee implements IPerson { private String cname; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public void displayInfo() { System.out.println("Employee Company is:" + this.cname); } } INJECT PROEPRTIES 4/6/2022 package com.snipe.learning.springcore.di; public class Student implements IPerson{ private String cname; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public void displayInfo() { System.out.println("Student college"+this.cname); } } <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee"> <property name="cname" value="SNIPE TECH PVT LTD"></property> </bean> <bean id="student" class="com.snipe.learning.springcore.di.Student"> <property name="cname" value="J.N.N.C.E"></property> </bean>
  • 23. 23 public class Employee { private String cname; private String design; private float salary; public Employee(String cname,String design, float salary) { this.cname = cname; this.design = design; this.salary = salary; } public void displayInfo() { System.out.println("Employee Company is:" + this.cname); System.out.println("Designation is:" + this.design); System.out.println("Salary is:" + this.salary); } } CONSTRUCTOR INJECTION 4/6/2022 package com.snipe.learning.springcore.di; public class Student implements IPerson { private String cname; private String std; private float Scholorship; public Student(String cname, String std, float scholorship) { this.cname = cname; this.std = std; this.Scholorship = scholorship; } public void displayInfo() { System.out.println("College is:" + this.cname); System.out.println("Standard is:" + this.std); System.out.println("Scholorship is:" + this.Scholorship); } } <bean id="employee" class="com.snipe.learning.springcore.di.Employee"> <constructor-arg name="cname" value="SNIPE TECH PVT LTD" /> <constructor-arg name="design" value="SOFTWARE ENGINEER" /> <constructor-arg name="salary" value="30000" /> </bean> <bean id="student" class="com.snipe.learning.springcore.di.Student"> <constructor-arg index="0" value="J.N.N.C.E" /> <constructor-arg index="1" value="MTech" /> <constructor-arg index="2" value="10000" /> </bean>
  • 24. 24 <!-- bean definitions here --> <bean id="employee“ class="com.snipe.learning.springcore.di.Employee"> <property name="cname" value="SNIPE TECH PVT LTD" /> <property name="design" value="SOFTWARE ENGINEER" /> <property name="salary" value="30000" /> <property name="address" ref="empAddress"></property> </bean> <bean id="student“ class="com.snipe.learning.springcore.di.Student"> <property name="cname" value="J.N.N.C.E" /> <property name="std" value="MTech" /> <property name="scholorship" value="10000" /> <property name="address" ref="stdAddress"></property> </bean> <bean id="empAddress“ class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> <bean id="stdAddress“ class="com.snipe.learning.springcore.di.Address"> <property name="street" value="NT Road" /> <property name="area" value="NAVULE" /> <property name="city" value="SHIMOGA" /> </bean> INJECT OBJECT 4/6/2022 public class Address { public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } private String street; private String area; private String city; }
  • 25. 25 <!-- bean definitions here --> <bean id="employee“ class="com.snipe.learning.springcore.di.Employee"> <property name="cname" value="SNIPE TECH PVT LTD" /> <property name="design" value="SOFTWARE ENGINEER" /> <property name="salary" value="30000" /> <property name="address" ref="empAddress"></property> </bean> <bean id="student“ class="com.snipe.learning.springcore.di.Student"> <property name="cname" value="J.N.N.C.E" /> <property name="std" value="MTech" /> <property name="scholorship" value="10000" /> <property name="address" ref="stdAddress"></property> </bean> <bean id="empAddress“ class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> <bean id="stdAddress“ class="com.snipe.learning.springcore.di.Address"> <property name="street" value="NT Road" /> <property name="area" value="NAVULE" /> <property name="city" value="SHIMOGA" /> </bean> INJECT OBJECT 4/6/2022 public class Address { public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } private String street; private String area; private String city; }
  • 26. 26 <?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee"> <property name="cname" value="SNIPE TECH PVT LTD" /> <property name="design" value="SOFTWARE ENGINEER" /> <property name="salary" value="30000" /> <property name="address"> <bean class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> </property> </bean> INNER BEAN, ALIAS 4/6/2022 <bean id="student" class="com.snipe.learning.springcore.di.Student" name="std_details"> <property name="cname" value="J.N.N.C.E" /> <property name="std" value="MTech" /> <property name="scholorship" value="10000" /> <property name="address" ref="stdAddress" /> </bean> <bean id="empAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> <bean id="stdAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="NT Road" /> <property name="area" value="NAVULE" /> <property name="city" value="SHIMOGA" /> </bean> <alias name="employee" alias="emp_details"></alias> </beans>
  • 27. 27 package com.snipe.learning.springcore.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.snipe.learning.springcore.di.Employee; import com.snipe.learning.springcore.di.Student; public class TestDIMAIN { public static void main(String[] args) { System.out.println("In CLASSPATH BEAN FACTORY"); BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml"); Employee employee = (Employee) factory.getBean("emp_details"); employee.displayInfo(); Student student = (Student) factory.getBean("std_details"); student.displayInfo(); } } INNER BEAN, ALIAS 4/6/2022
  • 28. 28 <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee"> <property name="addresses"> <list> <ref bean="homeAddress" /> <ref bean="officeAddress" /> </list> </property> </bean> <bean id="homeAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> <bean id="officeAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="NT Road" /> <property name="area" value="NAVULE" /> <property name="city" value="SHIMOGA" /> </bean> <alias name="employee" alias="emp_details"></alias> </beans> INJECTING COLLECTIONS 4/6/2022 package com.snipe.learning.springcore.di; import java.util.List; public class Employee implements IPerson { private List<Address> addresses; public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this.addresses = addresses; } public void displayInfo() { for (Address address : addresses) { System.out .println("Address is:" + address.getStreet() + "::" + address.getArea() + "::" + address.getCity()); } } }
  • 29. 29 autowire 4/6/2022 Autowiring is injecting object implicitly without external implementation. This works matching by Name, Type or constructor arguments. This works only for reference objects not for primitive or strings Mode Description no No autowiring default byName The byName mode injects the based on the name. In this case property name and bean name must be same. It internally calls setter method. byType The byType mode injects based on type object. So property name and bean name can be different. It internally calls setter method. It works only one bean exists. constructor The constructor mode injects by calling the constructor of the class. It calls the constructor having large number of parameters. It works only one bean of that class.
  • 30. 30 AUTOWIRED BY NAME 4/6/2022 <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee" autowire="byName"></bean> <bean id="home_addresses" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> <bean id="office_addresses" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="NT Road" /> <property name="area" value="NAVULE" /> <property name="city" value="SHIMOGA" /> </bean> </beans> package com.snipe.learning.springcore.di; public class Employee implements IPerson { private Address home_addresses; public Address getHome_addresses() { return home_addresses; } public void setHome_addresses(Address home_addresses){ this.home_addresses = home_addresses; } public Address getOffice_addresses() { return office_addresses; } public void setOffice_addresses(Address office_addresses) { this.office_addresses = office_addresses; } private Address office_addresses; public void displayInfo() { System.out.println("Employee Details"); System.out.println("Home address::"+this.home_addresses.getStreet()+" ::"+this.home_addresses.getArea()+" ::"+this.home_addresses.getCity()); System.out.println("Office address::"+this.office_addresses.getStreet()+" ::"+this.office_addresses.getArea()+" ::"+this.office_addresses.getCity()); } }
  • 31. 31 AUTOWIRED BY TYPE 4/6/2022 <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee" autowire="byType"></bean> <bean id="homeAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> </beans> package com.snipe.learning.springcore.di; public class Employee implements IPerson { private Address home_addresses; public Address getHome_addresses() { return home_addresses; } public void setHome_addresses(Address home_addresses) { this.home_addresses = home_addresses; } public void displayInfo() { System.out.println("Employee Details"); System.out.println("Home address::"+this.home_addresses.getStreet()+" ::"+this.home_addresses.getArea()+" ::"+this.home_addresses.getCity()); } } }
  • 32. 32 AUTOWIRED BY CONSTRUCTOR 4/6/2022 <!-- bean definitions here --> <bean id="employee" class="com.snipe.learning.springcore.di.Employee" autowire="constructor"> <constructor-arg name="cname" value="SNIPE TECH PVT LTD" /> <constructor-arg name="design" value="SOFTWARE ENGINEER" /> <constructor-arg name="salary" value="30000" /> </bean> </beans> package com.snipe.learning.springcore.di; public class Employee implements IPerson { private String cname; private String design; private float salary; public Employee(String cname, String design, float salary) { this.cname = cname; this.design = design; this.salary = salary; } public void displayInfo() { System.out.println("Employee Details::" + this.cname + " ::" + this.design + " ::" + this.salary); } } }
  • 33. 33 scope 4/6/2022 The Spring scopes are tells how many instances of beans are available to use it. By default only one bean instance will be created as and when context file loaded. Scopes are • Singleton – only one instance created during loading of context xml flile • Prototype –it creates more than one instance as and when the getBean method called. To each request the instance will be created. • Web aware context in spring • Request –spring on each servlet request bean will be created • Session –New bean per session • Global –session –New bean per one HTTP Request session(portlet context) <bean id="employee" class="com.snipe.learning.springcore.di.Employee" autowire="byType" scope="prototype"></bean> <bean id="homeAddress" class="com.snipe.learning.springcore.di.Address"> <property name="street" value="shatrugna marga" /> <property name="area" value="srirampura 2nd stage" /> <property name="city" value="mysuru" /> </bean> ANNOTATION @Bean @Scope("prototype")
  • 34. 34 ApplicationContextAware, BeanNameAware 4/6/2022 • BeanFactory instantiate bean when you call getBean() method while ApplicationContext instantiate Singleton bean when container is started, It doesn't wait for getBean() to be called. • BeanFactory doesn't provide support for internationalization but ApplicationContext provides support for it. • Another difference between BeanFactory vs ApplicationContext is ability to publish event to beans that are registered as listener. • One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext. • If you are using auto wiring and using BeanFactory than you need to register AutoWiredBeanPostProcessor using API which you can configure in XML if you are using ApplicationContext. In summary BeanFactory is OK for testing and non production use but ApplicationContext is more feature rich container implementation and should be favored over BeanFactory
  • 35. 35 ApplicationContextAware, BeanNameAware 4/6/2022 import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Student implements ApplicationContextAware, BeanNameAware { private ApplicationContext context = null; public ApplicationContext getContext() { return context; } @Override public void setBeanName(String bean) { // TODO Auto-generated method stub System.out.println("bean name::" + bean); } @Override public void setApplicationContext(ApplicationContext context) throws BeansException { // TODO Auto-generated method stub this.context = context; } }
  • 36. 36 INHERITANCE 4/6/2022 public class Person { protected String name; protected int age; protected String sex; .. } public class Student extends Person { private String cname; private String std; private float Scholorship; .. } <bean id="person" class="com.snipe.learning.springcore.di.Person"> <property name="name" value="Pranet M D" /> <property name="age" value="13" /> <property name="sex" value="Male" /> </bean> <bean id="student" class="com.snipe.learning.springcore.di.Student" parent="person"> <property name="cname" value="J.N.N.C.E" /> <property name="std" value="MTech" /> <property name="scholorship" value="10000" /> </bean>
  • 37. 37 LIFE CYCLE CALL BACK METHODS 4/6/2022 public class Student extends Person implements InitializingBean, DisposableBean { @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("Destry bean"); } public void afterPropertiesSet() throws Exception { System.out.println("Initializing bean"); } public void init_bean() { System.out.println("Init method"); } public void cleanup() { System.out.println("cleanup method"); } } <bean id="person" class="com.snipe.learning.springcore.di.Person"> <property name="name" value="Pranet M D" /> <property name="age" value="13" /> <property name="sex" value="Male" /> </bean> <bean id="student" class="com.snipe.learning.springcore.di.Student" parent="person" init-method="init_bean" destroy-method="cleanup"> <property name="cname" value="J.N.N.C.E" /> <property name="std" value="MTech" /> <property name="scholorship" value="10000" /> </bean>
  • 38. 38 INTERFACE USAGE 4/6/2022 package com.snipe.learning.springcore.di; public interface IPerson { public void displayInfo(); } package com.snipe.learning.springcore.di; public class Student implements IPerson { @Override public void displayInfo() { System.out.println("STUDENT INFORMATION"); } } package com.snipe.learning.springcore.di; public class Employee implements IPerson{ @Override public void displayInfo() { System.out.println("EMPLOYEE INFORMATION"); } } <!-- bean definitions here --> <!-- <bean id="person" class="com.snipe.learning.springcore.di.IPerson"/> --><bean id="student" class="com.snipe.learning.springcore.di.Student" /> <bean id="employee" class="com.snipe.learning.springcore.di.Employee" /> </beans> public class TestDIMAIN { public static void main(String[] args) { System.out.println("In CLASSPATH BEAN FACTORY"); AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); context.registerShutdownHook(); IPerson student = (Student)context.getBean("student"); student.displayInfo(); IPerson employee = (Employee)context.getBean("employee"); employee.displayInfo(); } }
  • 39. 39 SPRING AOP 4/6/2022 • Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. • Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.) • Objects have other than business model details like managing logging, transaction, security so on • This code required in all methods • We cannot manage or modify at once.
  • 40. Where use AOP  AOP is mostly used in following cases: • to provide declarative enterprise services such as declarative transaction management. • It allows users to implement custom aspects. Steps of AOP • Write Aspect • Configure Aspect 40 SPRING AOP 4/6/2022
  • 41. hierarchy of advice interfaces 41 SPRING AOP 4/6/2022
  • 42. Core AOP concepts  Join point An identifiable point in the execution of a program. Central, distinguishing concept in AOP  Pointcut Program construct that selects join points and collects context at those points.  Advice Code to be executed at a join point that has been selected by a pointcut  Introduction Additional data or method to existing types, implementing new interfaces 42 SPRING AOP 4/6/2022
  • 43. • Advice • Advice represents an action taken by an aspect at a particular join point. There are different types of advices: • Before Advice: it executes before a join point. • After Returning Advice: it executes after a joint point completes normally. • After Throwing Advice: it executes if method exits by throwing an exception. • After (finally) Advice: it executes after a join point regardless of join point exit whether normally or exceptional return. • Around Advice: It executes before and after a join point. 43 SPRING AOP 4/6/2022
  • 44. Spring AOP AspectJ Annotation • There are two ways to use Spring AOP AspectJ implementation: • By annotation: We are going to learn it here. • By xml configuration (schema based) 44 SPRING AOP 4/6/2022
  • 45. • Spring AspectJ AOP implementation provides many annotations: • @Aspect • @Pointcut: The annotations used to create advices are given below: • @Before • @After • @After Returning • @Around • @AfterThrowing 45 SPRING AOP 4/6/2022
  • 46. Bean class public class ExampleModel { public void read() { System.out.println("read info"); } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice.ExampleBeforeA spect"/> 46 @BEFORE 4/6/2022 @Aspect public class ExampleBeforeAspect { @Before("allread()") public void LoggingAdvice() { System.out.println("Logging Advice"); } @Pointcut("execution(public void read())") public void allread() {} } Output: Logging Advice read info
  • 47. Bean class public class ExampleModel { public void read() { System.out.println("read info"); } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice.ExampleAfterAs pect"/> 47 @AFTER 4/6/2022 @Aspect public class ExampleAfterAspect { @After("allread()") public void LoggingAdvice() { System.out.println("Logging Advice"); } @Pointcut("execution(public void read())") public void allread() {} } Output: read info Logging Advice
  • 48. Bean class public class ExampleModel { public void read() { System.out.println("read info"); //throw new RuntimeException(); } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice.ExampleAfterAs pect"/> 48 @AFTERRETURNING 4/6/2022 @Aspect public class ExampleAfterAspect { @AfterReturning("allread()") public void LoggingAdvice() { System.out.println("Logging Advice"); } @Pointcut("execution(public void read())") public void allread() {} } Output: read info Logging Advice
  • 49. Bean class public class ExampleModel { public void read() { System.out.println("read info"); int nume = 10/0; System.out.println(num); } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice.ExampleExceptio nAspect"/> 49 @AFTERTHROWING 4/6/2022 @Aspect public class ExampleExceptionAspect { @AfterReturning("allread()") public void ReturningAdvice() { System.out.println("Returning Advice"); } @AfterThrowing("allread()") public void ExceptionAdvice() { System.out.println("Exception Advice"); } @Pointcut("execution(public void read())") public void allread() {} } Output: read info Exception Advice Exception in thread "main" java.lang.ArithmeticException: / by zero
  • 50. Bean class package com.snipe.learning.aop.model; public class ExampleModel { public String readInfo(String name) { System.out.println("read info"); System.out.println(name); return name; } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice. ExampleAfteReturningAspect"/> 50 @AFTERRETURNING 4/6/2022 @@Aspect public class ExampleAfteReturningAspect { @AfterReturning("args(name)") public void LoggingAdvice(String name) { System.out.println("Logging Advice"+name); } @Pointcut("execution(public void read())") public void allread() {} } Output: read info mallikarjuna Logging Advicemallikarjuna
  • 51. Bean class package com.snipe.learning.aop.model; public class ExampleModel { public void readInfo() { System.out.println("read info"); } } applicationContext.xml <aop:aspectj-autoproxy/> <bean id="exampleModel" class="com.snipe.learning.aop.model.ExampleModel" /> <bean id="aspectWorkExample" class="com.snipe.learning.aop.aspectservice.ExampleAroundA spect"/> 51 @AROUND 4/6/2022 @Around("allread()") public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) { System.out.println("Logging Advice"); try { System.out.println("before advice"); proceedingJoinPoint.proceed(); System.out.println("after advice"); }catch(Throwable te) { System.out.println("After Throwing"); } } @Pointcut("execution(public void readInfo())") public void allread() {} Test :: ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); ExampleModel exampleModel =context.getBean ("exampleModel", ExampleModel.class); exampleModel.readInfo(); Output: Logging Advice before advice read info after advice
  • 52. 52 SPRING AOP 4/6/2022 @Aspect public class AspectWork { @Before("execution(public void readInfo())") public void LoggingAdvice() { System.out.println("Logging Advice"); } @Before("execution(public void printInfo())") public void PrintAdvice() { System.out.println("Print Advice"); } @Before("execution(* get*(..))") public void ReadAdvice() { System.out.println("Read advice"); } @After("execution(* get*(..))") public void WriteAdvice() { System.out.println("Write advice"); } } <?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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <aop:aspectj-autoproxy/> <bean id="student" class="com.snipe.learning.aop.model.Student"> <property name="name" value="Pranet M D" /> </bean> <bean id="employee" class="com.snipe.learning.aop.model.Employee"> <property name="name" value="Mallikarjuna G D" /> </bean> <bean id="person" class="com.snipe.learning.aop.model.Person" autowire="byName" /> <bean id="aspectWork" class="com.snipe.learning.aop.aspectservice.AspectWork"/> </beans>
  • 53. Data Access Model: Spring JdbcTemplate Problems of JDBC API The problems of JDBC API are as follows:  We need to write a lot of code before and after executing the query, such as creating connection, statement, closing resultset, connection etc.  We need to perform exception handling code on the database logic.  Repetition of all these codes from one to another database logic is a time consuming task. 53 Spring JdbcTemplate 4/6/2022
  • 54. Advantage of Spring JdbcTemplate • Spring JdbcTemplate eliminates all the above mentioned problems of JDBC API. It provides you methods to write the queries directly, so it saves a lot of work and time. 54 Spring JdbcTemplate 4/6/2022
  • 55. JdbcTemplate class  methods of spring JdbcTemplate class 55 Spring JdbcTemplate 4/6/2022
  • 56.  Spring Jdbc Approaches Spring framework provides following approaches for JDBC database access:  JdbcTemplate  NamedParameterJdbcTemplate  SimpleJdbcTemplate  SimpleJdbcInsert and SimpleJdbcCall 56 Spring JdbcTemplate 4/6/2022
  • 57. Spring MVC • In Spring Web MVC, DispatcherServlet class works as the front controller. It is responsible to manage the flow of the spring mvc application. 4/6/2022 57 Spring MVC
  • 58. Advantages of Spring 3.0 MVC  Supports RESTful URLs.  Annotation based configuration.  Supports to plug with other MVC frameworks like Struts etc.  Flexible in supporting different view types like JSP, velocity, XML, PDF etc., 4/6/2022 58 Spring MVC
  • 59. Front Controller - Responsiblities • Initialize the framework to cater to the requests. • Load the map of all the URLs and the components responsible to handle the request. • Prepare the map for the views. 4/6/2022 59 Spring MVC
  • 61.  Spring Boot File Structure  Spring Boot Simple Example  SPRING BOOT IN ECLIPSE 4/6/2022 61 CONTENTS
  • 62. • Spring Boot is a Framework from “The Spring Team” to ease the bootstrapping and development of new Spring Applications. 4/6/2022 62 INTRODUCTION • Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. • We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. • Most Spring Boot applications need very little Spring configuration.  INTRODUCTION
  • 63. What is Spring ? 4/6/2022 63 Application framework Programming and Configuration model Infrastructure support Challenges Looks like very vast framework Lot more setup and configuration hudles Lot more deployment modes
  • 64. What is Spring Boot ? • It is a Spring module which provides RAD (Rapid Application Development) feature to Spring framework. • It provides defaults for code and annotation configuration to quick start new Spring projects within no time. It follows “Opinionated Defaults Configuration” Approach to avoid lot of boilerplate code and configuration to improve Development, Unit Test and Integration Test Process. 4/6/2022 64 SPRING BOOT SPRING FRAMEWORK Embedded HHTP Servers, (Tomcat, Jetty) XML <Bean> Configuration
  • 65. WHY USE SPRING BOOT? • To ease the Java-based applications Development, Unit Test and Integration Test Process and Time. To increase Productivity. • To avoid XML Configuration completely. • To avoid defining more Annotation Configuration(It combined some existing Spring Framework Annotations to a simple and single Annotation) • To avoid writing lots of import statements • To provide some defaults to quick start new projects within no time. • To provide Opinionated Development approach. 4/6/2022 65 WHY USE SPRING BOOT?
  • 66. • To Setup default configuration • Starts a application context • Problems of class path scan • Starts tomcat server 4/6/2022 66 WHAT SPRING BOOT DOES
  • 67. Spring • Dependency Injection Framework • Manage Lifecycle Of Java(Beans) • Boiler plate configuration -programmer writes a lot of code to minimal task • Takes Time to have a spring application up and run Spring Boot • A suite of pre-configured frameworks and technologies -That is used to remove boilerplate configuration • The shortest way to have application up and running 4/6/2022 67
  • 68. Spring Boot Framework has mainly four major Components.  Spring Boot Starters: Spring Boot Starters is one of the major key features or components of Spring Boot Framework. The main responsibility of Spring Boot Starter is to combine a group of common or related dependencies into single dependencies.  Spring Boot Auto Configurator; AutoConfigurator is to reduce the Spring Configuration. If we develop Spring applications in Spring Boot, then We don't need to define single XML configuration and almost no or minimal Annotation configuration. Spring Boot Auto Configurator component will take care of providing those information.  Spring Boot CLI: Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application. 4/6/2022 68 COMPONENTS OF SPRING BOOT
  • 69.  Spring Boot Starters: The spring-boot-actuator module provides all of Spring Boot’s production- ready features. The recommended way to enable the features is to add a dependency on the spring-boot- starter-actuator “Starter”. 4/6/2022 69 COMPONENTS OF SPRING BOOT
  • 71. Its look like: 4/6/2022 71 EXAMPLES • Spring Boot Starters: example: if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency Pattern: spring-boot-starter-*, where * spring-boot-starter-web It is used for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container. spring-boot-starter-jdbc It is used for JDBC with the Tomcat JDBC connection pool. <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> COMPONENTS OF SPRING BOOT
  • 72. 4/6/2022 72 • Spring Boot Auto Configurator example; • Spring Boot Starter reduces build’s dependencies and Spring Boot AutoConfigurator reduces the Spring Configuration. • Spring Boot Starter has a dependency on Spring Boot AutoConfigurator, Spring Boot Starter triggers Spring Boot AutoConfigurator automatically. @SpringBootAppliction @Configuration @ComponentScan @EnableAutoConfig COMPONENTS OF SPRING BOOT
  • 73.  Spring Boot Starters: The spring-boot-actuator module provides all of Spring Boot’s production- ready features. The recommended way to enable the features is to add a dependency on the spring-boot- starter-actuator “Starter”. 4/6/2022 73 COMPONENTS OF SPRING BOOT
  • 74. 4/6/2022 74 SPRING BOOT STRUCTURE <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.snipe.learning.springboot</groupId> <artifactId>SpringBootApp</artifactId> <version>0.0.1-SNAPSHOT</version><name>my first learn</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.2</version> </parent> <properties><project.build.sourceEncoding>UTF- 8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target></properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
  • 75. . Bill of Materials version dependency management of all the dependent child jars • Embedded Tomcat Server – Manage the servlet container – Standalone run – Microservice architecture support – Developer free from lot of configurations 4/6/2022 75 HIDDEN TREASURE
  • 76.  Post Man 4/6/2022 76 We will use this postman application for using these services • GET • POST • PUT • DELETE SPRING BOOT IN ECLIPSE
  • 77. 4/6/2022 77 The spring boot can be booted through: Spring Boot CLI SPRING BOOT CLI
  • 79. 4/6/2022 79 • See the website for common application.properites spring application.properties
  • 80. 1) What is Spring? 2) What are the advantages of spring framework? 3) What are the modules of spring framework? 4) What is IOC and DI? 5) What is the role of IOC container in spring? 6) What are the types of IOC container in spring? 7) What is the difference between BeanFactory and ApplicationContext? 8) What is the difference between constructor injection and setter injection? 9) What is autowiring in spring? What are the autowiring modes? 10) What are the different bean scopes in spring? 11) In which scenario, you will use singleton and prototype scope? 80 Spring Interview Questions
  • 81. 12 What are the advantages of JdbcTemplate in spring? 13) What are the advantages of spring AOP? 14) What is interceptor? 15)Does spring perform weaving at compile time? 16) What are the AOP implementation? 17) What is the front controller class of Spring MVC? 18) What does @Controller annotation? 19) What does @RequestMapping annotation? 20) What does the ViewResolver class? 21) Which ViewResolver class is widely used? 22) Does spring MVC provide validation support? 81 Spring Interview Questions