The Spring Framework is an open-source application framework for Java that provides comprehensive infrastructure support for developing Java applications. It was created to simplify Java development and promote good design practices. Since its initial release in 2003, it has evolved into a robust platform that supports various programming paradigms, including object-oriented, aspect-oriented, and reactive programming.
Inversion of Control (IoC):
Dependency Injection (DI):
Aspect-Oriented Programming (AOP):
Spring Container:
Spring MVC:
Spring Boot:
The architecture of the Spring Framework is modular, consisting of several layers:
Core Container:
AOP Module:
Data Access/Integration:
Web Module:
Security:
Create a new Maven project with the following structure:
spring-example/
|-- pom.xml
|-- src/
|-- main/
|-- java/
| `-- com/example/spring/
| |-- config/
| | `-- AppConfig.java
| |-- controller/
| | `-- HelloController.java
| `-- model/
| `-- Greeting.java
|-- resources/
`-- application.properties
pom.xml
Here’s a basic pom.xml
file with Spring dependencies:
4.0.0
com.example
spring-example
1.0-SNAPSHOT
org.springframework
spring-webmvc
5.3.9
org.springframework
spring-context
5.3.9
javax.servlet
javax.servlet-api
4.0.1
provided
org.slf4j
slf4j-api
1.7.32
org.slf4j
slf4j-simple
1.7.32
AppConfig.java
This is the configuration class where we define our beans.
package com.example.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.spring")
public class AppConfig {
}
Greeting.java
This class represents the data model.
package com.example.spring.model;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
HelloController.java
This controller handles web requests
package com.example.spring.controller;
import com.example.spring.model.Greeting;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
public class HelloController {
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format("Hello, %s!", name));
}
}
You need to configure a web application initializer to bootstrap the Spring context.
WebInitializer.java
package com.example.spring;
import com.example.spring.config.AppConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
To run the application, you can deploy it on a servlet container like Apache Tomcat or use Spring Boot to run it as a standalone application.
If you’re using Spring Boot, the main
method in the application can be defined as follows:
package com.example.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringExampleApplication.class, args);
}
}
After starting the application, you can test it by navigating to:
http://localhost:8080/greeting?name=John
You should see a JSON response like this:
{
"id": 1,
"content": "Hello, John!"
}