SlideShare une entreprise Scribd logo
1  sur  83
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

Contenu connexe

Tendances

Tendances (20)

Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Connecting Connect with Spring Boot
Connecting Connect with Spring BootConnecting Connect with Spring Boot
Connecting Connect with Spring Boot
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
ASP.NET Routing & MVC
ASP.NET Routing & MVCASP.NET Routing & MVC
ASP.NET Routing & MVC
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Les dessous du framework spring
Les dessous du framework springLes dessous du framework spring
Les dessous du framework spring
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoC
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Java J2EE
Java J2EEJava J2EE
Java J2EE
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Spring boot
Spring bootSpring boot
Spring boot
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 

Similaire à IOC and DI in Spring Framework

Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaalifha12
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlArjun Thakur
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaJignesh Aakoliya
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdfDeoDuaNaoHet
 
Designing Java EE Applications in the Age of CDI
Designing Java EE Applications in the Age of CDIDesigning Java EE Applications in the Age of CDI
Designing Java EE Applications in the Age of CDIMichel Graciano
 
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin StantescuRefactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin StantescuPayU Türkiye
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkRajind Ruparathna
 
What's New In Apache Lenya 1.4
What's New In Apache Lenya 1.4What's New In Apache Lenya 1.4
What's New In Apache Lenya 1.4nobby
 
Microservices Primer for Monolithic Devs
Microservices Primer for Monolithic DevsMicroservices Primer for Monolithic Devs
Microservices Primer for Monolithic DevsLloyd Faulkner
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise Group
 
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...Edureka!
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalMarian Wamsiedel
 
Folio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 Software
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web FrameworkLuther Baker
 
Salesforce lwc development workshops session #6
Salesforce lwc development workshops  session #6Salesforce lwc development workshops  session #6
Salesforce lwc development workshops session #6Rahul Gawale
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 

Similaire à IOC and DI in Spring Framework (20)

Spring framework core
Spring framework coreSpring framework core
Spring framework core
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
 
An Overview of Entity Framework
An Overview of Entity FrameworkAn Overview of Entity Framework
An Overview of Entity Framework
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company india
 
2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf2-0. Spring ecosytem.pdf
2-0. Spring ecosytem.pdf
 
Designing Java EE Applications in the Age of CDI
Designing Java EE Applications in the Age of CDIDesigning Java EE Applications in the Age of CDI
Designing Java EE Applications in the Age of CDI
 
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin StantescuRefactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
 
Spring Framework -I
Spring Framework -ISpring Framework -I
Spring Framework -I
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
What's New In Apache Lenya 1.4
What's New In Apache Lenya 1.4What's New In Apache Lenya 1.4
What's New In Apache Lenya 1.4
 
Microservices Primer for Monolithic Devs
Microservices Primer for Monolithic DevsMicroservices Primer for Monolithic Devs
Microservices Primer for Monolithic Devs
 
Skillwise-Spring framework 1
Skillwise-Spring framework 1Skillwise-Spring framework 1
Skillwise-Spring framework 1
 
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
Spring Framework Tutorial | Spring Tutorial For Beginners With Examples | Jav...
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 
Folio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP YiiFolio3 - An Introduction to PHP Yii
Folio3 - An Introduction to PHP Yii
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 
Salesforce lwc development workshops session #6
Salesforce lwc development workshops  session #6Salesforce lwc development workshops  session #6
Salesforce lwc development workshops session #6
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 

Plus de Mallikarjuna G D (20)

Reactjs
ReactjsReactjs
Reactjs
 
Bootstrap 5 ppt
Bootstrap 5 pptBootstrap 5 ppt
Bootstrap 5 ppt
 
CSS
CSSCSS
CSS
 
Angular 2.0
Angular  2.0Angular  2.0
Angular 2.0
 
Hibernate
HibernateHibernate
Hibernate
 
Jspprogramming
JspprogrammingJspprogramming
Jspprogramming
 
Servlet programming
Servlet programmingServlet programming
Servlet programming
 
Servlet programming
Servlet programmingServlet programming
Servlet programming
 
Mmg logistics edu-final
Mmg  logistics edu-finalMmg  logistics edu-final
Mmg logistics edu-final
 
Interview preparation net_asp_csharp
Interview preparation net_asp_csharpInterview preparation net_asp_csharp
Interview preparation net_asp_csharp
 
Interview preparation devops
Interview preparation devopsInterview preparation devops
Interview preparation devops
 
Interview preparation testing
Interview preparation testingInterview preparation testing
Interview preparation testing
 
Interview preparation data_science
Interview preparation data_scienceInterview preparation data_science
Interview preparation data_science
 
Enterprunership
EnterprunershipEnterprunership
Enterprunership
 
Core java
Core javaCore java
Core java
 
Type script
Type scriptType script
Type script
 
Angularj2.0
Angularj2.0Angularj2.0
Angularj2.0
 
Git Overview
Git OverviewGit Overview
Git Overview
 
Jenkins
JenkinsJenkins
Jenkins
 
Hadoop
HadoopHadoop
Hadoop
 

Dernier

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Dernier (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

IOC and DI in Spring Framework

  • 1. Reach me @ : Mallikarjuna G D gdmallikarjuna@gmail.com 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