The basic concept of the Inversion of Control pattern is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. In a typical IOC scenario, the container creates all the objects, wires them together by setting the necessary properties, and determines when methods will be invoked.
There are two types of DI,
- Setter Injection
- Constructor Injection
Setter Injection:-
Spring framework will inject the dependency via a setter method.
XML configuration for setter Dependency Injection is below.
<bean id="classBean" class="com.adnjava.ClassBean">
<!-- setter injection using the nested <ref/> element -->
<property name="studentBean"><ref bean="studentBean"/></property>
</bean>
<bean id="studentBean" class="com.adnjava.StudentBean"/>
The ClassBean java POJO class is as below,
package com.adnjava; public class ClassBean { private StudentBean studentBean; public void setStudentBean(StudentBean studentBean){ this.studentBean=studentBean; } public void getStudentBean(){ return studentBean; } }
Constructor Injection:-
Here Spring uses the Constructor and the arguments passed to it to determine the dependency. Rest all is same as setter injection.XML configuration for Constructor Dependency Injection is below.
<bean id="classBean" class="com.adnjava.ClassBean">
<constructor-arg><ref bean="studentBean"/></constructor-arg>
<!--OR you can use <constructor-arg ref="yetAnotherBean"/>-->
</bean>
<bean id="studentBean" class="com.adnjava.StudentBean"/>
The ClassBean java POJO class is as below,
package com.adnjava;
public class ClassBean {
private StudentBean studentBean;
public ClassBean(StudentBean studentBean){
this.studentBean=studentBean;
}
public void getStudentBean(){
return studentBean;
}
}
Interface Injection:
This is not implemented in Spring currently, but by Avalon. It’s a different type of DI that involves mapping items to inject to specific interfaces.Advantages Of Dependency Injection:
- Loosely couple code
- Separation of responsibility
- Configuration and code is separate.
- Using configuration, you can provide implemented code without changing the dependent code.
- Testing can be performed using mock objects.
Related Post:--
Spring MVC workflow with example
Spring MVC with Hibernate CRUD Example
Spring Annotations
Web Server
A web server primarily serves static resources and forwards dynamic requests to an application server if needed.
Responsibilities:
Serves static content (HTML, CSS, JavaScript, images, PDFs, etc.)
Handles HTTP/HTTPS requests
Supports SSL/TLS termination
Performs URL rewriting and request routing
Can act as a reverse proxy and load balancer
Examples:
Apache HTTP Server
Nginx
Microsoft IIS
Application Server
An application server executes the application's business logic and generates dynamic content.
Responsibilities:
Executes Java, .NET, or other server-side applications
Processes business logic
Connects to databases
Manages transactions and security
Generates dynamic responses
Supports technologies such as Servlets, JSP, EJB, and REST APIs
Examples:
Apache Tomcat
JBoss EAP
IBM WebSphere Application Server
Oracle WebLogic Server
Key Differences
In Spring Boot
With Spring Boot, the distinction is often less visible because it includes an embedded servlet container such as Apache Tomcat by default. This allows you to package and run your application without installing a separate application server.
For production environments, many organizations still place a web server such as Nginx or Apache HTTP Server in front of the Spring Boot application to handle SSL termination, load balancing, caching, and reverse proxying.