Spring Course 2024v2
Spring Course 2024v2
Spring
I. Introduction
Spring is a Java framework for building web applications.
- Spring Boot: Takes an opinionated view of building Spring applications and gets you up and
running as quickly as possible.
- Spring Framework: Provides core support for dependency injection, transaction management,
web apps, data access, messaging, and more.
- Spring Data: Provides a consistent approach to data access – relational, non-relational, map-
reduce, and beyond.
- Spring Cloud: Provides a set of tools for common patterns in distributed systems. Useful for
building and deploying microservices.
- Spring Cloud Data Flow: Provides an orchestration service for composable data microservice
applications on modern runtimes.
- Spring Security: Protects your application with comprehensive and extensible authentication
and authorization support.
- Spring Authorization Server: Provides a secure, light-weight, and customizable foundation for
building OpenID Connect 1.0 Identity Providers and OAuth2 Authorization Server products.
- Spring for GraphQL: Spring for GraphQL provides support for Spring applications built on
GraphQL Java.
- Spring Session: Provides an API and implementations for managing a user’s session information.
- Spring Integration: Supports the well-known Enterprise Integration Patterns through lightweight
messaging and declarative adapters.
- Spring Web Services: Facilitates the development of contract-first SOAP web services.
See: https://spring.io/projects
The term "Spring" means different things in different contexts. It can be used to refer to the
Spring Framework project itself. Most often, when people say "Spring", they mean the entire
family of projects. (link: https://docs.spring.io/spring-framework/reference/overview.html)
1
II. Spring Framework
The current version of Spring Framework is 6.1.3. As of Spring Framework 6.0, Spring requires Java 17+.
The Spring Framework uses Beans. It supports the Dependency Injection and Common Annotations.
Features:
Core technologies: dependency injection, events, resources, i18n, validation, data binding, type
conversion, SpEL, AOP.
Integration: remoting, JMS, JCA, JMX, email, tasks, scheduling, cache and observability.
1. Annotations
@Entity
The at sign character (@) indicates to the compiler that what follows is an annotation. In the following
example, the annotation's name is Override:
@Override
void mySuperMethod() {
}
2
The annotation can include elements, which can be named or unnamed, and there are values for those
elements:
or
@SuppressWarnings(value = "unchecked")
void myMethod() {
}
If there is just one element named value, then the name can be omitted, as in:
@SuppressWarnings("unchecked")
void myMethod() {
}
If the annotation has no elements, then the parentheses can be omitted, as shown in the previous
@Override example.
The annotation type can be one of the types that are already defined. In the previous examples,
Override and SuppressWarnings are predefined Java annotations. It is also possible to define your own
annotation type. The Author and Ebook annotations in the previous example are custom annotations.
Annotations can be applied to declarations: declarations of classes, fields, methods, and other program
elements.
3
2. Beans
@RestController
public class HomeController {
@GetMapping
public String index() {
return "hello world";
}
}
Example:
@SpringBootApplication
public class WebapiApplication {
public static void main(String[] args) {
SpringApplication.run(WebapiApplication.class, args);
}
}
3. Dependency injection
Dependency injection (DI) is a process where objects define their dependencies (that is, the other
objects they need). The container then injects those dependencies when it creates the bean.
4
A class can define its dependencies in different ways:
- constructor arguments
- arguments to a method, or
- fields annotated with @Autowired
The following example shows a class that can be dependency-injected with @Autowired annotation:
Spring is mainly used to create REST Web Services (RESTful Web API). It lets you create special
@RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to
HTTP by using @RequestMapping annotations.
The following code shows a typical @RestController that serves JSON data:
5
@RestController
@RequestMapping("/users")
public class MyRestController {
private final UserRepository userRepository;
@GetMapping
public List<User> getUsers() {
return this.userRepository.findAll();
}
@GetMapping("/{userId}")
public User getUser(@PathVariable Long userId) {
return this.userRepository.findById(userId).get();
}
@PostMapping
public void createUser(@RequestBody User user) {
// create user
}
@PutMapping("/{userId}")
public User updateUser(@PathVariable Long userId, @RequestBody User user)
{
// update user
}
@DeleteMapping("/{userId}")
public void deleteUser(@PathVariable Long userId) {
this.userRepository.deleteById(userId);
}
}
5. Spring MVC
The Spring Web MVC framework (often referred to as “Spring MVC”) is a rich “model view controller”
web framework.
Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP
requests.
6
Spring MVC allows us to serve dynamic HTML content. It supports a variety of templating technologies,
including:
- Thymeleaf
- JSP
- FreeMarker
- Groovy
- Mustache
If possible, JSPs should be avoided. There are several known limitations when using them with
embedded servlet containers.
When you use one of these templating engines with the default configuration, your templates are
picked up automatically from src/main/resources/templates.
Spring Boot makes it easy to create Spring based Applications that you can "just run".
Features
Getting Started
7
1. Create new project
The easiest way to create a new project is using Spring initializr: https://start.spring.io/
We choose Maven as the project building tool, and Java as the programming language
- Group : name of the application owner (or his reversed domain name)
- Artifact: the application name (Artifact = Name)
Generate the Zip file, extract it and open it using your IDE
8
2. Project Structure
Required tools:
- IDE
o
IntelliJ IDE
o
Eclipse IDE for Enterprise Java and Web Developers:
https://www.eclipse.org/downloads/packages/
o VS Code + Java Extension Pack + Spring Boot Extension Pack
- Postman
9
TP 1
10