Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views31 pages

Spring Core Overview

The document outlines the evolution of Java EE and Spring frameworks, detailing various versions and their features from J2EE 1.2 to Java EE 7 and Spring 1.0 to 4.3. It explains concepts such as Inversion of Control (IoC), Dependency Injection (DI), and the types of Spring containers, along with the necessary software setup for Spring development. Additionally, it covers manual and auto-wiring techniques for dependency management in Spring applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views31 pages

Spring Core Overview

The document outlines the evolution of Java EE and Spring frameworks, detailing various versions and their features from J2EE 1.2 to Java EE 7 and Spring 1.0 to 4.3. It explains concepts such as Inversion of Control (IoC), Dependency Injection (DI), and the types of Spring containers, along with the necessary software setup for Spring development. Additionally, it covers manual and auto-wiring techniques for dependency management in Spring applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

History

Client side Server side Server side business


Database
presentation presentation logic

EJB V1 & V2 complexity : Early version of EJB are very complex


J2EE 1.2 – Servlets, JSP, EJB, RMI – 1999 Multiple deployment descriptors
J2EE 1.3 – EJB, CMP, JCA – 2001 Multiple interfaces
Poor performance of Entity Beans
J2EE 1.4 – Webservices mgmt., deployments – 2003 EJB Client
Java EE 5 – Ease of use of EJB3, JPA, JSF, JAXB, JAX-WS -2006 Home Interface
Home Object
Java EE 6 – Ease of use of JAX-RS(Rest), CDI – 2009 Component Interface
EJB Object
Java EE 7 – JMS-2, Batch, TX, Concurrency, Websockets – 2013 Bean class

J2EE development without EJB, Rod Johnson, Founder of Spring


Spring 1.0 – 2004
Spring 2.0 – 2006
Spring 3.0 – 2009
Spring 4.0 – 2013
Spring 4.3 – 2016
Spring
• Spring is not only for dependency injection but now is used for Enterprise
application framework
• Spring is programming with configurations model
• Spring is used for Infrastructure support and simulates a cloud environment

Beans Core AOP Aspects

Context SPEL Instrumentation

JDBC ORM JMS


web servlet WebSocket

Portlet
Spring course required softwares

Software's to be setup in system:


• Java 8(JDK 8) --- Programming language
https://www.oracle.com/in/java/technologies/javase/javase-jdk8-downloads.html#license-lightbox
• Eclipse R Java EE edition or Neon version - IDE
https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/2021-03/R/eclipse-java-2021-03-R-w
in32-x86_64.zip
• Maven 3 – Build tool and also downloads all the dependencies required like spring, hibernate
libraries from maven central repo to our system for the first time
https://apachemirror.wuchna.com/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.zip
• MySQL 5.5 or 8 - Database
https://downloads.mysql.com/archives/community/
• Tomcat 8 server – Application server
https://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.66/bin/apache-tomcat-8.5.66.zip
• Advanced rest client tool or postman – This is used to invoke rest webservices
https://install.advancedrestclient.com/install
• Core spring(spring ioc, aop) - Java 8, Maven 3, Eclipse
• Hibernate - Java 8, Maven 3, Eclipse, MySQL
• Spring MVC - Java 8, Maven 3, Eclipse, MySQL, Tomcat 8
• Spring Rest - Java 8, Maven 3, Eclipse, MySQL, Tomcat 8,
PostMan/Advanced Rest client
Spring IOC
• Inversion of control:
• The design pattern responsible for handling the creation of objects defined
in the configuration and providing that to application
Application context
This will be or Spring Container
Service classes
taken care by
spring
framework

Application Factory class


Create the objects by reading
Return the configuration
the
object
Define what
Will use the service class objects to be
objects where these Configuration created
objects are created by
Spring container using Annotation based
the configuration XML based configuration
config
Dependency Injection
DI is responsible for creating the dependencies and injecting the dependency between the objects

Create object X, but X object is dependent on Y object

Y object has to be created and injected into X object

Employee object, but Employee object is dependent on Department object

Department object is created and injected into Employee object – a relation has been established where
Employee belongs to Department or Employee is associated with department

Injection – passing the object to the other object or wiring

Spring framework = IOC + DI = IOC

IOC is responsible for creating the objects that are defined in the configuration and also uses DI to create
dependencies between the objects and inject to the application
Who is handling the complete control of creating the objects --- IOC

IOC is handled by Spring Container that manages the life cycle of an object right
from creation of the object to the destruction of the object. It use DI(Dependency injection) to manage the
dependencies between the objects
Spring IOC Container
Spring IOC container along with DI will create and manage the life cycle of objects till the object destruction

Spring provides 2 types of containers


1. Bean Factory Container
2. Application Context Container

BeanFactory is lightweight
ApplicationContext is more preferred way and also recommended as it has support for event listeners during bean life cycle
management

Resource resource = new FileSystemResource(“appbeans.xml”);


BeanFactory factory = new XmlBeanFactory(resource);
ClassPathResource resource = new ClassPathResource(“apbeans.xml”);
BeanFactory factory = new XmlBeanFactory(resource);

Application Context types:


FileSystemXmlApplicationContext : This container loads the definitions of the beans from an xml file and takes the full path
of xml file into the constructor argument
ClassPathXmlApplicationContext: This container loads the definitions of the beans from an xml file and takes the path of xml
file from the classpath
WebXmlApplicationContext: This loads the XML file with definitions of all beans from within a application
Types of Dependency Injection
Instantiation bean using a static factory method
class A {
Constructor Injection private static A a;
public static A getInstance() {
Setter Method Injection if(a==null)
a = new A();
return a;}
Constructor arg: }
<constructor-arg> In xml
<ref bean= “org” > <bean id=“a” class=“A” factory-method=“getInstance”/>

</constructor-arg> Instantiation bean using constructor


class A {
A() { }
Setter Method Injection: }
<property name="department"> In xml
<bean id=“a” class=“A”/>
<ref bean="dept" />
</property> Instantiation bean using instance factory method
class A {
public A getInstance() {
The property name should match return new A();
with the setter method property }
}
name of the class In xml
The ref bean value should match <bean id=“a” class=“A” factory-method=“getInstance”/>
with bean id value in the xml file
Java Virtual Machine

Heap memory Stack memory


Application Context/spring
container
emp1

Employee Employee Department Department


employee1 employee2 department1 department2

<bean id="employee1”
class="com.springcoredemo2.Employee“/> Employee emp1=
context.getBean(“employee1”)
<bean id="employee2”
class="com.springcoredemo2.Employee“/>
An object can be injected into another
<bean id=“department1” object in 2 ways
class="com.springcoredemo2.Department“/> a) Setter method
b) Constructor
<bean id=“department2”
class="com.springcoredemo2.Department“/>
employee.getDeparment

Employee constructor or
setDepartment method of
Employee class
Employee Department
employee1 department1

Configuration Spring class Employee {


Container private Department department;

public void setDepartment(Department


department) {
<bean id="employee1”
class="com.springcoredemo2.Employee“> this.department = department;
}
<property name=“department” ref=“department1”/>
</bean>

<bean id=“department1”
class="com.springcoredemo2.Department“/>
employee.getDepartment1

employee.getDepartment

Employee constructor or
setDepartment method of
Employee class
Employee Department Department
employee1 department1

Configuration Spring
Container
class Employee {
private Department
department;
<bean id="employee1”
public Employee(Department
class="com.springcoredemo2.Employee“> department) {
<constructor-arg name=“department” ref=“department1”/>
</bean> this.department =
department;
<bean id=“department1” }
class="com.springcoredemo2.Department“/>
Manual wiring –(wiring) – injecting one object to the other object

We will specify what objects to be injected to the other object in form of property or constructor arg

Auto wiring -- auto wiring will solve all the manual dependencies specified in the configuration and makes the
configuration will simpler.
Auto wiring in xml :
byType : The class type in the setter method is matched with the bean class in the configuration where the bean
is injected into the setter method of the bean where autowire byType is defined
If more than 1 bean is found with same class in the xml configuration, autowire byType give error
The fix for this is in annotations

byName : The property name in the setter method is matched with the bean id in the configuration where the
bean is injected into the setter method of the bean where autowire byName is defined

Constructor: Constructor also works like byType where it matches the class type
with the bean class defined in the configuration
byType matches the setter method class type parameter with the bean class type in the configuration(xml)
Constructor matches the constructor argument class type with the bean class type in the configuration(xml)
Constructor autowiring – xml

Constructor with arguments of all public NewOrganization(List<NewDepartment> departments,


If there are more than 2 beans found with List data types is matched and
different class types repeated to be injected List<AdvancedDepartment> departments1) {
executed }

If not present
<bean id="newdepartment" public NewOrganization(List<NewDepartment> departments) {
Constructor taking single arguments }
class="com.springcoredemoautowring1.NewDepartment"/ of either list data type is matched or
> and executed public NewOrganization(List<AdvancedDepartment>
If no present departments) {
<bean id="advanceddepartment" }
If any other parameterized
class="com.springcoredemoautowring1.AdvancedDepartm constructor present other than
ent"/> above, Unique bean matching error

<bean id="advanceddepartment1"
If No constructor present, no bean
class="com.springcoredemoautowring1.AdvancedDepartm gets injected
ent"/>

<bean id="newdepartment1" This flow will be applicable if the bean id’s are
class="com.springcoredemoautowring1.NewDepartment"/ different with the constructor argument names
>

<bean id="org2"
class="com.springcoredemoautowring1.NewOrganization"
autowire="constructor" />
Constructor autowiring- xml

This flow will be applicable if the bean id’s are


different with the constructor argument names

If there is one bean defined with 1 class type and Constructor with single class type public NewOrganization(NewDepartment departments,
more than 1 bean of other class type is defined in parameter and list of repeated data type List<AdvancedDepartment> departments1) {
configuration objects is matched and executed }
If not present
<bean id="newdepartment" Constructor with all arguments of list of public NewOrganization(List<NewDepartment> departments,
class="com.springcoredemoautowring1.NewDepartment"/> different data type objects is matched and List<AdvancedDepartment> departments1) {
executed }
<bean id="advanceddepartment" If not present
class="com.springcoredemoautowring1.AdvancedDepartment"/>
Constructor taking argument of single data public NewOrganization(NewDepartment department) {
type object is matched and executed }
<bean id="advanceddepartment1"
class="com.springcoredemoautowring1.AdvancedDepartment"/> If not present
public NewOrganization(List<NewDepartment> departments) {
<bean id="org2" Constructor taking single List argument of }
either data type is matched and executed or
class="com.springcoredemoautowring1.NewOrganization" public NewOrganization(List<AdvancedDepartment>
autowire="constructor" /> departments) {
}
If any other parameterized constructor
present other than above, Unique bean
matching error

If No constructor present, no bean gets


injected
Constructor autowiring - xml

This flow will be applicable if the bean id’s are


different with the constructor argument names

If there is exactly one bean defined with 1 class Constructor with one data type object and public NewOrganization(NewDepartment departments,
type and 1 bean of other class type defined in another data type object AdvancedDepartment departments1) {
configuration }
If not present
<bean id="newdepartment" public NewOrganization(List<NewDepartment> departments,
AdvancedDepartment departments1) {
class="com.springcoredemoautowring1.NewDepartment"/> Constructor with arguments of list of one
}
data type object and another single data
Or public NewOrganization(NewDepartment departments,
type object
<bean id="advanceddepartment" List<AdvancedDepartment> departments1) {
If not present }
class="com.springcoredemoautowring1.AdvancedDepartment"/>
Constructor with all arguments of lists of public NewOrganization(List<NewDepartment> departments,
<bean id="org2" different data types is matched and List<AdvancedDepartment> departments1) {
class="com.springcoredemoautowring1.NewOrganization" executed }
If not present
autowire="constructor" />
Constructor taking single argument of public NewOrganization(NewDepartment newDepartment) {
either data type object is matched and } or
executed public NewOrganization(AdvancedDepartment advepartment) {
}
If not present
public NewOrganization(List<NewDepartment> newDepartment) {
Constructor taking single argument list of } or
either data type is matched and executed public NewOrganization(List<AdvancedDepartment> advepartment) {
}

If No constructor present, no bean gets


injected
Genetic Constructor auto wiring flow - xml
class NewOrganization{

public NewOrganization(List<NewDepartment> newdeptlist, List<AdvancedDepartment> advdeptlist){


}

public NewOrganization(List<NewDepartment> newdeptlist, AdvancedDepartment department1){


}

public NewOrganization(NewDepartment department1, List<AdvancedDepartment> advdep){


}

public NewOrganization(NewDepartment department1b, AdvancedDepartment department2b){


}

public NewOrganization(List<NewDepartment> newdeptlist) {


}

public NewOrganization(NewDepartment department1b) {


}

public NewOrganization(AdvancedDepartment department2b) {


}
}
<bean id="department1" class="com.springcoredemoautowiring2.NewDepartment"/>
<bean id="department1a" class="com.springcoredemoautowiring2.NewDepartment"/>
<bean id="department2" class="com.springcoredemoautowiring2.AdvancedDepartment"/>
<bean id="department2a" class="com.springcoredemoautowiring2.AdvancedDepartment"/>
<bean id="organization1" class="com.springcoredemoautowiring2.NewOrganization1" autowire="constructor"/>
Generic Constructor auto wiring flow - xml
Greedy pattern approach :
 Spring will see for the bean id's matching with constructor arguments(parameters) along with types

Matching preferences

• Spring will check for max arguments constructor(n arguments, this depends on no. of classes)

• All of the constructor argument names matching with bean id and class types

• Any of the constructor argument name matching with bean id and class type

• Constructor that has arguments taking list arguments

• Repeat the steps for (n-1) arguments constructor


ByType autowring - xml
If there are setter methods of List
If there are more than 1 bean with same class to public void
argument type for all the repeated
be injected setAdvanceddepartments(List<AdvancedDepartment>
matching bean class types. The
advanceddepartments) {
<bean id="advanceddepartment" setter methods should contain only
}
class="com.springcoredemoautowring1.AdvancedDepartment"/> the setter methods with list
parameteres otherwise error
<bean id="advanceddepartment1"
class="com.springcoredemoautowring1.AdvancedDepartment"/>

<bean id="org2" class="com.springcoredemoautowring1.NewOrganization" If there are setter methods of List public void
autowire="constructor" /> argument type for all the matching setAdvanceddepartments(List<AdvancedDepartment>
repeated bean class types. All these advanceddepartments) {
methods will be called }
If there are more than one bean and the class
types are unique
<bean id="advanceddepartment"
class="com.springcoredemoautowring1.AdvancedDepartment"/> public void setAdvanceddepartments(AdvancedDepartment
If there are setter methods of advanceddepartments) {
matching bean type, all these }
<bean id="org2" class="com.springcoredemoautowring1.NewOrganization"
autowire="constructor" />
methods will be called

If parameterized constructor is found in the class and the bean configuration does not contain autowire
by constructor, an error will be thrown
Application Context – Default singleton and Eager loading
JVM The beans are by default
Eager loading
Heap memory And also singleton
NewDepartment-
department1 context.getBean(“department1”)
Application
Context—context1 context.getBean(“department1”)
AdvDepartment-
department2 context.getBean(“department2”)

NewDepartment- context1.getBean(“department1”);
Application department1
Context --context
AdvDepartment-
department2

ApplicationContext context = new


ClassPathXmlApplicationContext("appcontext.xml");

Creating applictaion context or spring container object

ApplicationContext context1 = new


ClassPathXmlApplicationContext("appcontext.xml");
Bean Scopes and Loading
Singleton – Same object returned for the same bean id per application context

Prototype – Different object returned for the same bean id per application context
By default spring beans are singleton and eager loading.

<bean id="department"
class="com.springcorebeanscopes.Department"/>

<bean id=“department1"
ApplicationContext context = new
class="com.springcorebeanscopes.Department"/>
ClassPathXmlApplicationContext("appcontext.xml");

context.getBean("department") – This returns object id 1(example)

context.getBean("department") – This returns object id 1(example)

context.getBean("department1") – This returns object id 2(example)

ApplicationContext context = new


ClassPathXmlApplicationContext("appcontext.xml");
Bean Scopes and Loading
Eager Loading – Spring Beans are created during the application context creation

Lazy loading – Spring Beans are created during the context.getBean creation

Scope Eager Loading Lazy Loading

Singleton Beans are created during application context Beans are created during
creation. The context.getBean on the same bean id context.getBean. The context.getBean on
returns the same object for any number of calls the same bean id returns the same object
for any number of calls

Prototype Beans are created during context.getBean. The Beans are created during
context.getBean on the same bean id returns the context.getBean. The context.getBean on
different object for each call the same bean id returns the different
object for each call

For prototype, irrespective of Eager loading or Lazy loading, the bean is created during context.getBean only
Bean Scopes Loading and Dependency Objects

Department bean scopes and loading Employee Singleton and Eager

Employee is dependent on
Department: Singleton and Eager Employee and department Object are Department
created during application context only

Department: Singleton and Lazy Employee and department Object are


created during application context only class Employee {
Department department
Department: Prototype and Eager Employee and department Object are
created during application context only }

Department: Prototype and Lazy Employee and department Object are


created during application context only

Department bean scopes and loading Employee Singleton and Lazy

Department: Singleton and Eager Department Object is created during application context only.
Employee object is created during context.getBean

Department: Singleton and Lazy Employee object is created during context.getBean


And at the same time department object is also created

Department: Prototype and Eager Employee object is created during context.getBean


And at the same time department object is also created

Department: Prototype and Lazy Employee object is created during context.getBean


And at the same time department object is also created
Bean Scopes Loading and Dependency Objects

Department bean scopes and loading Employee Prototype and Eager

Department: Singleton and Eager Department Object is created during application context only.
Employee object is created during context.getBean
Department: Singleton and Lazy Employee object is created during context.getBean
And at the same time department object is also created
Department: Prototype and Eager Employee object is created during context.getBean
And at the same time department object is also created
Department: Prototype and Lazy Employee object is created during context.getBean
And at the same time department object is also created

Department bean scopes and loading Employee Prototype and Lazy

Department: Singleton and Eager Department Object is created during application context only.
Employee object is created during context.getBean
Department: Singleton and Lazy Employee object is created during context.getBean
And at the same time department object is also created
Department: Prototype and Eager Employee object is created during context.getBean
And at the same time department object is also created
Department: Prototype and Lazy Employee object is created during context.getBean
And at the same time department object is also created
Summary for dependency beans loading

• If department is eager and singleton, department object is created during application context creation only
irrespective of employee object creation(eager(applictaion context creation) or lazy(context.getBean) and (singleton
or prototype))

• If department is (lazy and singleton) or (prototype(eager or singleton)), department object is created during employee
object creation(employee object can be created eagerly or lazily and (singleton or prototype))

Examples:

1. department is lazy and singleton


employee object is eager

department object is created along with employee object creation(during application context creation)

2. department is lazy and singleton


employee object is lazy

department object is created along with employee object creation(during context.getBean)


Bean scopes

Case 1 :
Required object(Department) : eager and singleton class Employee {
Dependent Object(Employee): singleton and eager
Department department;
Created during application
context creation
}
Department
context.getBean(“department”)

context.getBean(“department”)
Employee
emp1.getDepartment();
Employee emp1 = context.getBean(“employee”)
emp2.getDepartment();
Employee emp2 = context.getBean(“employee”)
Created during application
context creation
Bean scopes
Case 2 :
Required object(Department) : prototype (Lazy or Eager) Dependent Object(Employee): singleton and eager

Created during employee


context.getBean(“department”) Department object creation

Department Department
context.getBean(“department”)
emp2.getDepartment()
Employee emp1=context.getBean(“employee”) Employee emp1.getDepartment()
Created during application context
Employee emp2=context.getBean(“employee”) creation

Case 3 :
Required object(Department) : singleton and Lazy Dependent Object(Employee): prototype and Lazy or Eager
Created during employee object creation

context.getBean(“department”) Department
context.getBean(“department”) Created during context.getBean

Employee emp1.getDepartment() emp2.getDepartment()


Employee emp1 = context.getBean(“employee”)
Employee emp2 = context.getBean(“employee”) Employee
Created during context.getBean
Bean scopes

Case 4 :
Required object(Department) : prototype and eager/lazy Dependent Object(Employee): prototype and
eager/lazy

context.getBean(“department”) Department Created during employee Created during employee


object creation object creation

Department Department
Department
context.getBean(“department”)
Created during context.getBean

Employee emp1 = context.getBean(“employee”) Employee emp1.getDepartment()

emp2.getDepartment()
Employee emp2 = context.getBean(“employee”) Employee
Created during context.getBean
Annotation based configuration
Important annotations in Spring IOC and DI
@Configuration : @Configuration indicates that the class has @Bean definition methods or @Component scan to scan for the
@Component so that Spring Container can process the class and generates Spring Beans tied to application Context

@Bean : Method level annotation used to explicitly declare a Spring Bean

@Component: Class Level annotation which will be detected by @ComponentScan for creating Spring Beans

@Autowired: Enables to inject the object dependency, can be used on Setter method, field, Constructor

@Qualifier: if there is more than 1 bean with same class type, using @Qualifier we can match the bean id to be injected
@Qualifier is placed along with @Autowired

@Primary: If we can declare @Primary in any of the @Bean/@Component, then by default the bean
with @Primary gets injected if there is a conflict in autowiring of the beans with same class type provided there is no @Qualifier matching
present

@Scope : Indicates the name of the bean scope to be used like Prototype(default is singleton if no @Scope is present). Can be used along
with @Bean or @Component

@Lazy : Indicates the lazy loading of Bean , can be used along with @Bean or @Component

@ComponentScan: Scans for the @Components in the specified base package, default base package is the package where @Configuration
class is present

@Order: Specifies the order of loading the bean by Spring Container


Bean Life cycle

Container is Dependency Internal spring


Bean Instantiation
started Injection processing

If there is any
custom custom-init methd

Bean is ready to
use
Container is Bean destroy method
destroyed is called and bean is
also killed
Bean Life Cycle- In detail
Calling
@PostConstruct
Constructor
Context.close
Initializing Bean
interface method
Dependency
Injection(calling
setter methods) –
Custom init method Bean destroy is
If any
called(@PreDestro
y)

Calling Bean
Calling BeanPostProcessing:
Aware methods postProcessAfterInitial
ization

Disposable Bean
Interface method

Calling BeanPostProcessing: Bean is ready to use


postProcessBeforeInitia
lization

Internal spring processing means : calling BeanAware


methods, calling bean post processing methods
@Autowired on Constructor

case 1:
in the class with @Component
suppose if we have exactly 1 parameterized constructor, this gets called even if we have @Autowired
or do'nt have @Autowired on the constructor

case 2:
in the class with @Component if we have more than 1 parametrized constructor, then one of the
constructor should be @Autowired otherwise it will throw error

case 3:
in the class with @Component if we have more than 1 parametrized constructor, then if we decalre
@Autowired
on more than 1 parametrized constructor, it will throw error

case 4:
in the class with @Component if we have 1 parmeterized constructor and 1 default constructor(constructor
without arguments) then default constructor gets called if the parmeterized constructor does
not have @Autowired otherwise the parameterized constructor gets called where @Autowired is present

You might also like