Spring-Framework-Introduction
Spring-Framework-Introduction
2. Spring IoC
3. Spring Bean
4. Dependency Injection
5. Autowiring in Spring
6.
5. Question and Answer
✓ ORM (object-relational mapping APIs): integrate with JPA, JDO, Hibernate, and iBatis.
✓ OXM (Object/XML mapping) implemente for JAXB, Castor, XMLBeans, JiBX and
XStream.
✓ Web-Struts: contains the support classes for integrating a classic Struts web tier (struts 1
or struts 2) within a Spring application
▪ Test
✓ The Test module supports the testing of Spring components with JUnit or TestNG
One main difference between BeanFactory and ApplicationContext is that BeanFactory only
instantiates bean when we call getBean() method while ApplicationContext instantiates singleton bean
when the container is started, It doesn't wait for getBean() method to be called.
BeanFactory is the root interface of Spring IOC container. ApplicationContext is the child
interface of BeanFactory interface that provides Spring AOP features, i18n etc.
package com.fsoft.bean;
public Employee() {
//getter-setter methods
}
package com.fsoft.bean;
Employee
-------------------------- public class Client {
- empId Client public static void main(String[] args) {
- empName ------------------------- Employee employee = new Employee();
- address + main()
-------------------------- employee.setEmpId(1);
+ getter employee.setEmpName("John Watson");
+ setter
employee.setAddress("New York");
System.out.println("Employee details: " + employee);
}
}
Employee
----------------
- empId Client
- empName -------------
- address Container + main()
----------------
+ getter
+ setter
✓ Annotation-based configuration
✓ Java-based configuration
▪ JDK - 17 or later
▪ Maven - 3.2+
▪ IDE - Eclipse/STS
</beans>
AppLogUtils.getLog().info(book);
}
}
Result
[INFO ] 2023-08-23 11:18:35 [main] Main 20 - Book(bookId=1, title=Java SE Programming Language, year=2023, version=2)
The Spring IoC container by using Java POJO classes and configuration metadata
procedures a fully configured and executable system or application.
Spring Beans
Spring IoC container instantiates, assembles, and manages the bean object.
The configuration metadata that are supplied to the container are used create Beans
object.
class This attribute is mandatory and specify the bean class to be used to create the bean.
This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you
name
use the id and/or name attributes to specify the bean identifier(s).
scope This attribute specifies the scope of the objects created from a particular bean definition.
constructor-arg This is used to inject the dependencies and will be discussed in subsequent chapters.
A lazy-initialized bean tells the IoC container to create a bean instance when it is first
lazy-init
requested, rather than at startup.
public Address() {
// getter-setter methods
}
Spring DI
It is a design pattern which removes the dependency from the programming code, that makes
the Application easy to manage and test.
Dependency Injection makes our programming code loosely coupled, which means change in
implementation doesn't affects the use.
public Employee() {
this.empId = 0;
this.empName = "N/A";
this.address = new Address();
}
}
DI
Interface
Constructor Setter/Getter Service Locator
Implementation
Autowiring in Spring
..
<bean id="department" class="fa.training.entities.Department">
<property name="deptName" value="Information Technology" />
</bean>
Output:
Employee Id : 100
Employee Name : 100
Department : Information Technology
..
<bean id="dept" class="fa.training.entities.Department">
<property name="deptName" value="Information Technology" />
</bean>
class Employee {
public Employee(Department department) {
super();
this.department = department;
}
}