Mahika Tutorials sharing Java Spring Framework Tutorials. You can visit our YouTube Page for Video Session also : https://www.youtube.com/c/mahikatutorials
4. Spring is a light weight and open source framework created by Rod
Johnson in 2003.
Mahika Tutorials
Spring Application
Spring jars
5. Spring framework makes the development of Java/JavaEE application
easy.
Mahika Tutorials
Dtababase Connectivity
Transaction Management
Dependency Injection
AOP
6. Spring can be thought of as a framework of frameworks because it provides
support to various frameworks such as Struts, Hibernate, EJB, JSF etc.
Mahika Tutorials
Spring
JSF
EJB
Hibernate
Struts
7. Spring enables you to build applications from “plain old Java objects”
(POJOs).
Mahika Tutorials
8. Spring framework is said to be a non-invasive means it doesn’t force a
programmer to extend or implement their class from any predefined
class or interface given by Spring API.
Mahika Tutorials
9. Spring applications are loosely coupled because of dependency
injection and Inversion of Control(IOC).
Mahika Tutorials
Employee
Address address;
Employee(){
address=newAddress(“XYZ Street ,”Pune”);
}
Employee
Address address;
Third Party
Address
instance
Tight Coupling Loose Coupling
11. Core Container
Mahika Tutorials
Core and Beans:These modules provide IOC and Dependency Injection features.
Context:This module supports internationalization (I18N), EJB, JMS, Basic Remoting.
SpEL: It is an extension to the EL defined in JSP. It provides support to setting and getting property values,
method invocation, accessing collections, named variables, logical and arithmetic operators, retrieval of
objects by name etc.
12. Data Access / Integration-
Mahika Tutorials
These modules basically provide support to interact with the database.
The JDBC module removes the need to do tedious JDBC coding.
ORM module is used to tie up with ORM tools such as hibernate.
13. Web
Mahika Tutorials
These modules provide support to create web application.
Test
This module supports the testing of Spring components with JUnit orTestNG.
16. Dependency Injection
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their
dependencies.
A class X has a dependency to classY, if class X uses classY as a variable.
Java components / classes should be as independent as possible of other Java classes.
This increases the possibility to reuse these classes and to test them independently of other classes(UnitTesting).
Dependency injection is a style of object configuration in which an object’s fields are set by an external entity, in other
words objects are configured by an external entity.
Dependency injection is an alternative to having the object configure itself.
Mahika Tutorials
class X
{
Y y;
……………
}
18. Inversion of Control is a principle by which the control of objects is transferred to a
container or framework.
Dependency injection is a pattern through which IoC is implemented, where the control
being inverted is the setting of object’s dependencies.
The act of connecting objects with other objects, or “injecting” objects into other objects,
is done by container rather than by the objects themselves.
A class should not configure itself but should be configured from outside.
Mahika Tutorials
Dependency Injection and Inversion of Control
19. Dependency Injection and Inversion of Control
IOC makes the code loosely coupled. In such case, there is no need to
modify the code if our logic is moved to new environment.
IOC makes the application easy to test.
In Spring framework, IOC container is responsible to inject the
dependency. We provide metadata to the IOC container either by XML
file or annotation
Mahika Tutorials
20. Dependency Injection
Spring framework provides two ways to inject dependency
By Constructor (Constructor Injection)-In the case of constructor-based dependency
injection, the container will invoke a constructor with arguments each representing a
dependency we want to set.
By Setter method (Setter Injection)-For setter-based DI, the container will call setter
methods of our class.
Mahika Tutorials
class Employee{
Address address;
Employee(){
address=newAddress(“XYZ Street”, ”Pune”, ”MH”);
}
…………………..
}
With IOC (setter Injection)Without IOC
class Employee{
Address address;
public void setAddress(Address address){
this.address=address;
}
………
}
With IOC (constructor Injection)
class Employee{
Address address;
Employee(Address address){
this.address=address;
………
}
}
23. IOC
In Spring framework, IOC container is responsible to inject the dependencies.We provide metadata
to the IOC container either by XML file or annotation.
The IoC container is responsible to instantiate, configure and assemble the objects.
Mahika Tutorials
BeanFactory
ApplicationContext
IOC
containers
24. BeanFactory
Mahika Tutorials
Spring BeanFactory Container is the simplest container which provides basic support for DI.
It is defined by org.springframework.beans.factory.BeanFactory interface.
There are many implementations of BeanFactory interface . The most commonly used
BeanFactory implementation is –
org.springframework.beans.factory.xml.XmlBeanFactory
Example-
Resource resource=new ClassPathResource(“Beans.xml");
BeanFactory factory=new XmlBeanFactory(resource);
The Resource interface has many implementaions. Two mainly used are:
1)org.springframework.core.io.FileSystemResource :Loads the resource from underlying file
system.
Example-
BeanFactory bfObj = new XmlBeanFactory(new FileSystemResource ("c:/beansconfig.xml"));
2)org.springframework.core.io.ClassPathResource:Loads the resource from classpath.
25. ApplicationContext
The ApplicationContext container is Spring’s advanced container.
It is defined by org.springframework.context.ApplicationContext interface.
The ApplicationContext interface is built on top of the BeanFactory interface.
It adds some extra functionality than BeanFactory such as simple integration with
Spring's AOP, message resource handling (for I18N), event propagation etc.
There are many implementations of ApplicationContext interface .The most
commonly used ApplicationContext implementation is –
org.springframework.context.support.ClassPathXmlApplicationContext
Example-
ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
Mahika Tutorials
28. Autowiring in Spring
Mahika Tutorials
Autowiring means injecting the object dependency implicitly.
It internally uses setter or constructor injection.
It works with reference only.
Autowiring can't be used to inject primitive and string values.
By default autowiring is disabled in spring framework.
Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.
31. Autowiring modes
Mahika Tutorials
No. Mode Description
1) no It is the default autowiring mode. It means no autowiring bydefault.
2) byName The byName mode injects the object dependency according to name of the bean. In such
case, property name and bean name must be same. It internally calls setter method.
3) byType The byType mode injects the object dependency according to type. So property name and
bean name can be different. It internally calls setter method.
4) constructor The constructor mode injects the dependency by calling the constructor of the class. It calls
the constructor having large number of parameters.
5) autodetect It is deprecated since Spring 3.
34. Autowiring in Spring
Mahika Tutorials
Autowiring means injecting the object dependency implicitly.
It internally uses setter or constructor injection.
It works with reference only.
Autowiring can't be used to inject primitive and string values.
By default autowiring is disabled in spring framework.
Autowiring can be performed by either using “autowire” attribute in <bean> or by using @Autowired annotation.