Spring Core Overview
Spring Core Overview
Portlet
Spring course required softwares
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
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
BeanFactory is lightweight
ApplicationContext is more preferred way and also recommended as it has support for event listeners during bean life cycle
management
<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
<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
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
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 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) {
}
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
<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
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");
Lazy loading – Spring Beans are created during the context.getBean creation
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
Employee is dependent on
Department: Singleton and Eager Employee and department Object are Department
created during application context only
Department: Singleton and Eager Department Object is created during application context only.
Employee object is created during context.getBean
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: 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:
department object is created along with employee object creation(during application context creation)
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
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
Case 4 :
Required object(Department) : prototype and eager/lazy Dependent Object(Employee): prototype and
eager/lazy
Department Department
Department
context.getBean(“department”)
Created during context.getBean
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
@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
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
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