0% found this document useful (0 votes)
4 views

4_Spring_REST_API

Chapter 4 covers the fundamentals of Spring Boot API, including Spring Beans, Dependency Injection, and the Application Context. It details the configuration of beans using XML, Java, and annotations, as well as concepts like validation, logging, and profiles. Additionally, it introduces Data Transfer Objects (DTOs) and their advantages, along with the integration of Swagger for API documentation.

Uploaded by

Sen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

4_Spring_REST_API

Chapter 4 covers the fundamentals of Spring Boot API, including Spring Beans, Dependency Injection, and the Application Context. It details the configuration of beans using XML, Java, and annotations, as well as concepts like validation, logging, and profiles. Additionally, it introduces Data Transfer Objects (DTOs) and their advantages, along with the integration of Swagger for API documentation.

Uploaded by

Sen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

CHAPTER 4

SPRING BOOT - API

1
Chapter 4: SPRING BOOT - API

OUTLINE
1. Spring Beans (Build beans vs User-defined beans)
2. Dependency Injection
3. Application Context
4. IoC Container (Inversion of Control)
5. Validation
6. Logging
7. Profiles

2
What is Beans?

3
What is Beans? (cont.)

4
Spring Bean Life Cycle
Container Beans Dependencies Internal Spring Your Custom
started Instantiated Injected Processing Init Method

Bean is Ready for Use


.
.
.
Your Custom Container is Shutdown
Destroy Method (at a certain point)

5
User Defined Spring Beans
1. @Controller, @Component, @Service, @Repository
2. @Bean, @Configuration
@Scope: singleton, prototype, request, session, global-session

Maven

Gradle implementation 'org.springframework:spring-context:6.2.0'


6
User Defined Spring Beans
▪ The primary job of the ApplicationContext is to manage beans → application
must provide the bean configuration to the ApplicationContext container.
▪ Type of configurations:
• XML-Based Configuration: ClassPathXmlApplicationContext (xml file)
• Annotation-Based Configuration: AnnotationConfigApplicationContext
• Java-Based Configuration: GenericWebApplicationContext

7
Configuring Beans in the Container
XML-Based Configuration – Setter Injection
src/main/resources/beans.xml

8
Configuring Beans in the Container
XML-Based Configuration – Setter Injection – Object Injection
src/main/resources/beans.xml

9
Configuring Beans in the Container
XML-Based Configuration – Setter Injection – Object Injection

10
Configuring Beans in the Container
XML-Based Configuration – Collection Injection

11
Configuring Beans in the Container
XML-Based Configuration – Literal Values Injection

12
Spring’s Auto-wiring (cont.)

explicit

Can be omitted

Find bean with the id = “faculty”

13
Spring’s Auto-wiring (cont.)

Find bean with the type “Faculty”

14
Spring’s Auto-wiring (cont.)

Find bean with the type “Faculty”


but it looks for the class type of the constructor arguments

15
Configuring Beans in the Container
Java-Based Configuration(cont.)

16
Configuring Beans in the Container
Java-Based Configuration(cont.)

17
Configuring Beans in the Container
Java-Based Configuration(cont.)

18
Configuring Beans in the Container
Annotation-Based Configuration
Create the XML configuration, applicationContext.xml (src/main/resources) to
enable annotations:

You can also use @Configuration


19
annotation with the same purpose
Configuring Beans in the Container
Annotation-Based Configuration (cont.)
<context:annotation-config/> only looks for annotations on beans in the
same application context in which it is defined.
You can also use @Configuration annotation with the same purpose

@Configuration
@ComponentScan(“fit.se.annotationbased")
public class AppConfig {

20
@Autowired xml config

Field Constructor
Setter

21
@Autowired Java-based config

22
@Autowired Disambiguation

23
@Autowired Disambiguation (cont.)
Error Caused by:
org.springframework.beans.fact
ory.NoSuchBeanDefinitionExcep
tion: No qualifying bean of type
'iuh.fit.se.autowired.disambigua
tion.

24
Disambiguation solution: @Qualifier

When there are multiple beans of the same type → use @Qualifier to avoid
ambiguity.
Spring uses the bean's name as a default qualifier value.
25
Disambiguation solution: @Primary

@Primary indicates that a particular bean should be given preference when


multiple beans are candidates to be autowired to a single-valued dependency. 26
Inject resources with @Value
@Configuration public class ClientBean {
@ComponentScan(”iuh.fit.se.resources") @Value("classpath:beans.xml")
@PropertySource("classpath:application.properties private Resource myResource;
")
public class AppConfig { @Value("${foo.permission}")
@Bean private String permission;
public ClientBean clientBean() {
return new ClientBean(); public void doSomething() throws IOException {
} File file = myResource.getFile();
} String s = new
String(Files.readAllBytes(file.toPath()));
public class Main { System.out.println(s);
public static void main(String[] args) throws IOException { System.out.println(permission);
AnnotationConfigApplicationContext context = }
new }
AnnotationConfigApplicationContext(AppConfig.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}
} 27
Spring REST API
Ref: 4_Spring_REST_API_Tutorial

28
29
Validation
https://jakarta.ee/learn/docs/jakartaee-tutorial/current/beanvalidation/bean-validation/bean-validation.html

https://jakarta.ee/learn/docs/jakartaee-tutorial/current/beanvalidation/bean-
validation/bean-validation.html

30
Validation (cont.)

31
Validation (cont.)

32
Validation (cont.)

33
Data Transfer Object - DTO

34
What is Data Transfer Object?
▪ A data transfer object (DTO) is an object that carries data between
processes.
▪ It is particularly useful when you want to transfer data across different
layers of an application or over the network.
▪ DTOs normally are created as POJOs. They are flat data structures that
contain no business logic. They only contain storage, accessors and
eventually methods related to serialization or parsing.

35
Data Transfer Object - Advantages
▪ Advantages
• Decouple business logic from the communication layer.
• Hide unnecessary data or protect sensitive information.
• Avoid multiple calls to the remote server.
• Prevent breaking changes in the API during updates of the application
model.
▪ Dis-advantages:
• Overhead: Can introduce additional layers of complexity.
• Requires mapping: Often requires conversion from and to POJOs or
entities.

36
What is Data Transfer Object?

37
Data Transfer Object - Example

38
Data Transfer Object – Example (cont.)

39
Logging
Ref: https://docs.spring.io/spring-
boot/reference/features/logging.html

40
Logging

41
Logging (cont.)

42
Profiles
Ref: https://docs.spring.io/spring-
boot/reference/features/profiles.html

43
Profiles
▪ Spring Profiles provide a way to segregate parts of your application
configuration and make it be available only in certain environments.

44
Profiles

45
Swagger
springdoc-openapi
Ref: https://springdoc.org

46
Swagger – springdoc-openapi

application.properties

47
Swagger – springdoc-openapi (cont.)

48
Swagger – springdoc-openapi (cont.)

49
Q&A

50

You might also like