Spring Framework Basics and IOC Overview
Spring Framework Basics and IOC Overview
Spring Core
What is Spring?
1. Framework Of Frameworks→ Because we can easily integrate with multiple
frameworks (Ex. Hibernate, JDBC)
Spring Framework I 1
2. It is a modularized framework
5. It is a non-invasive Framework.
Spring Framework I 2
Chapter 1
Introduction
Dependency Injection(D.I.)
We have seen Secondary references in the class. We injected another class
manually and we created the objects using a new keyword manually. But in the
Spring framework, We have an IOC container that will automatically inject the
required dependencies, and create objects as well.
Application Layers
Explain how to inject dependencies in different layers.
Spring Framework I 3
Chapter 2
Use:
To create the object, and hold the object in memory.
DI as per requirements
2. ApplicationContext(I)
Spring Framework I 4
BeanFactory (I) or Core Container
Implemented class is XmlBeanFactory.
1. ClassPathXmlApplicationContext
2. FileSystemXmlApplicationContext
3. AnnotationConfigWebApplicationContext
Spring Framework I 5
Doesn’t Support
Support Internationalization
Internationalization
2. Spring Aspect
3. Spring Beans
4. Spring Context
7. Spring Core
8. Spring Expression
9. Spring Instrument
Examples:
Step 2: Add all required jar files for Spring IOC in the build path.
Step 5:- Create Test class use Core container(Bean Factory) in [Link]
package
Spring Framework I 6
Package [Link]
Student
public Student() {
[Link]("Constructor get called!!");
}
</bean>
</beans>
Package [Link]
Test Class
import [Link];
import [Link];
import [Link];
import [Link];
Spring Framework I 7
import [Link];
//Beanfactory OC
BeanFactory bf=new XmlBeanFactory(r);
Student st = (Student)[Link]("stu");
[Link]("Constructor get called it means
beans get created automatically");
OutPut:
Constructor get called!!
Constructor get called it means beans get created automatically
Test
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
Spring Framework I 8
public class Test {
public static void main(String[] args) {
ApplicationContext apc=
new ClassPathXmlApplicationContext("[Link]");
Student st = (Student)[Link]("stu");
[Link]("Constructor get called it
means beans get created automatically");
Output:
Constructor get called!!
Constructor get called it means beans get created automatically.
Chapter 3
Dependency Injection
Spring Framework I 9
Ways to inject the dependency:
1. Setter-based injection/ Property injection
Datatypes/Dependencies:
1. Primitive Type
2. Collection Type
Primitive Type DI
Student class
package [Link];
Spring Framework I 10
}
[Link]
</bean>
</beans>
Test class
package [Link];
import [Link];
import [Link]
import [Link];
import [Link];
Spring Framework I 11
import [Link];
Student st = (Student)[Link]("stu");
[Link]([Link]());
[Link]([Link]());
101
Aboli
Secondary type DI
Student class
package [Link];
Spring Framework I 12
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
[Link] = addr;
}
Address Class
package [Link];
Spring Framework I 13
}
[Link]
</beans>
Test class
package [Link];
import [Link];
import [Link]
Spring Framework I 14
import [Link];
import [Link];
import [Link];
Student st = (Student)[Link]("stu");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]().getAddId());
[Link]([Link]().getAddName());
Collection Type DI
Student class
package [Link];
import [Link];
import [Link];
import [Link];
Spring Framework I 15
//generate getter Setter
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
[Link] = list;
}
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
[Link] = set;
}
public Map<Integer, String> getMap() {
return map;
}
public void setMap(Map<Integer, String> map) {
[Link] = map;
}
[Link]
Spring Framework I 16
<bean id="stu" class="[Link]">
<property name="map">
<map>
<entry key="111" value="Omkar"></entry>
<entry key="222" value="Sameer"></entry>
<entry key="333" value="Sam"></entry>
</map>
</property>
</bean>
Spring Framework I 17
</beans>
Test Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
Student st = (Student)[Link]("stu");
List<String> list = [Link]();
for(String s:list) {
[Link](s);
}
Set<Integer> set = [Link]();
for(Integer i:set) {
[Link](i);
}
Spring Framework I 18
Map<Integer, String> map = [Link]();
Set<Integer> keys = [Link]();
for(Integer key:keys) {
String value = [Link](key);
[Link](value);
}
Aboli
Akshada
Pratiksha
555
444
333
Omkar
Sameer
Sam
package [Link];
import [Link];
import [Link];
import [Link];
Spring Framework I 19
private String name;
[Link]
Test Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
Spring Framework I 20
import [Link];
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st = (Student) [Link]("stu");
[Link](st);
Secondary Reference DI
Student class
package [Link];
import [Link];
import [Link];
import [Link];
//contructor
public Student(int id, String name, Address addr) {
super();
[Link] = id;
Spring Framework I 21
[Link] = name;
[Link] = addr;
}
//toString
@Override
public String toString() {
return "Student [id="+id+",name="+name+",addr="+addr+"]";
}
Address class
package [Link];
//constructor
public Address(int addId, String addName) {
super();
[Link] = addId;
[Link] = addName;
}
//toString
@Override
public String toString() {
return "Address[addId="+addId+",addName="+addName+"]";
}
Spring Framework I 22
[Link]
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st = (Student) [Link]("stu");
[Link](st);
Spring Framework I 23
}
Chapter 4
2. Session
3. Global Session
Spring Framework I 24
Proof
Student Class
//getter setter
[Link]
Test Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
Spring Framework I 25
public static void main(String[] args) {
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
[Link]([Link]());
[Link]([Link]());
1234776885
1234776885
Test class
Spring Framework I 26
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st1 = (Student) [Link]("stu");
Student st2 = (Student) [Link]("stu");
[Link]([Link]());
[Link]([Link]());
1776957250
1268066861
Spring Framework I 27
Chapter 5
Autowiring
Spring container can inject secondary dependency by using Autowiring.
3. Constructor
By name:
This will inject the dependency according to name of the bean.
Student class
package [Link];
import [Link];
import [Link];
import [Link];
//getter setter
public int getId() {
Spring Framework I 28
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
[Link] = addr;
}
Address Class
package [Link];
//getter setter
public int getAddId() {
return addId;
}
public void setAddId(int addId) {
[Link] = addId;
}
public String getAddName() {
Spring Framework I 29
return addName;
}
public void setAddName(String addName) {
[Link] = addName;
}
[Link]
</bean>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
Spring Framework I 30
import [Link];
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st = (Student) [Link]("stu");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]().getAddId());
[Link]([Link]().getAddName());
101
Aboli
50001
pune
byType
Here dependency is injected as per the type of bean.(Class of bean)
But there must be only one object of that type. Otherwise ambiguity will be
there.
Example:
Refer the Student and Address class, Test class from above
[Link]
Spring Framework I 31
<bean id="ad" class="[Link]">
<property name="addId" value="50001"></property>
<property name="addName" value="pune"></property>
</bean>
</bean>
Note:
If multiple bean with same class are found, ambiguity will occur. To resolve this
add tag
Example:
Use same Student ,Address and Test class as above
[Link]
Spring Framework I 32
<property name="addName" value="Mumbai"></property>
</bean>
</bean>
Constructor
The constructor mode injects the dependency by calling the constructor of the
class.
student class
package [Link];
import [Link];
import [Link];
import [Link];
//constructor
public Student(int id, String name, Address addr) {
super();
Spring Framework I 33
[Link] = id;
[Link] = name;
[Link] = addr;
}
//toString
@Override
public String toString() {
return "Student[id="+id+",name="+name+",addr="+addr+"]";
}
Address Class
package [Link];
[Link]
Spring Framework I 34
<bean id="ad" class="[Link]">
<constructor-arg name="addId" value="50001">
</constructor-arg>
<constructor-arg name="addName" value="pune">
</constructor-arg>
</bean>
</bean>
Student class
package [Link];
import [Link];
import [Link];
import [Link];
Spring Framework I 35
import [Link];
@Autowired //******
private Address addr;
//getter setter
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
[Link] = addr;
}
Spring Framework I 36
Address Class
package [Link];
//getter setter
public int getAddId() {
return addId;
}
public void setAddId(int addId) {
[Link] = addId;
}
public String getAddName() {
return addName;
}
public void setAddName(String addName) {
[Link] = addName;
}
[Link]
<bean class="[Link].
[Link]">
</bean>
Spring Framework I 37
</bean>
</bean>
Test class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st = (Student) [Link]("stu");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]().getAddId());
[Link]([Link]().getAddName());
}
Spring Framework I 38
}
101
Aboli
50001
pune
Student class
@Autowired
@Qualifier("ad1")
Spring Framework I 39
private Address addr;
Chapter 6
Singleton Class
What is Singleton Class?
A singleton class means we can create only one object of that class.
It is a design pattern.
A class
package Singleton_Class;
public class A {
//private variable
private static final A a=new A();
//private constructor
private A()
{
[Link]("private constructor");
Spring Framework I 40
}
//Factory Method
public static A getA(){
[Link]("factory method ");
return a;
}
public void msg(){
[Link]("hello user");
}
[Link]
Test Class
package Singleton_Class;
import [Link];
import [Link]
Spring Framework I 41
private constructor
factory method
hello user
Chapter 7
Stereotype Annotations
We can replace the XML configuration by the Annotations
Example 1:
Student Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Spring Framework I 42
import [Link];
@Component("stu")
public class Student {
@Value("101")
private int id;
@Value("Aboli")
private String name;
// --------------------
// generate getter setter
public int getId() {
return id;
}
[Link]
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:context="[Link]
Spring Framework I 43
xmlns:p="[Link]
xsi:schemaLocation="
[Link]
[Link]
[Link]
[Link]
<context:component-scan base-package="[Link]"/>
</beans>
Test Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]
import [Link];
import [Link];
import [Link];
ApplicationContext apc =
new ClassPathXmlApplicationContext("[Link]");
Student st1 = (Student) [Link]("stu");
[Link]([Link]());
Spring Framework I 44
[Link]([Link]());
101
Aboli
@Component("stu")
public class Student {
public Student() {
[Link]("Student class Constructor");
}
}
JavaConfig class
@Configuration
@ComponentScan("[Link]")
public class JavaConfig {
Test Class
ApplicationContext apc =
Spring Framework I 45
new AnnotationConfigApplicationContext([Link]);
Student st1 = (Student) [Link]("stu");
public Student() {
[Link]("Student class Constructor");
}
}
JavaConfig Class
@Configuration
public class JavaConfig {
@Bean("stu")
public Student getStudent() {
Student s=new Student();
return s;
}
}
Test class
Spring Framework I 46
public class Test {
public static void main(String[] args) {
ApplicationContext apc =
new AnnotationConfigApplicationContext([Link]);
Student st1 = (Student) [Link]("stu");
Spring Framework I 47