SlideShare a Scribd company logo
Spring Guide
1.
Layer VS Domain
Layer
Layer
Domain
domain :
global :
Infra:
api : . rest api
api .
domain : .
Embeddable, Enum
.
dto : Request, Response .
exception : Exception
.
Domain
global
.
common : Value
. Request,
Response .
config : .
error : .
Exception Guide .
util : .
global
infra
.
common : Value
. Request,
Response .
config : .
error : .
Exception Guide .
util : .
infra
스프링 실전 가이드
Repository
Aggregate
스프링 실전 가이드
->
, ->
2. Lombok
Lombok
Lombok
Lombok .
… …
@Data @ToString, @EqualsAndHashCode, @Getter, @Setter,
@RequiredArgsConstructor …
…
@Data
Setter
.
@Setter
Member Coupon ToString .
JPA Json .
ToString
ToString .
@ToString(of = {“…”}) .
ToString
@EqualsAndHashCode
equals ( )
Set
HashCode
Lombok *.class
@EqualsAndHashCode ..
@Builder
@AllArgsConstructor
.
@Builder
.
,
.
@Builder
,
, .
Builder
.
@Builder.Default Builder .
POJO
@Builder.Default
JPA
PROTECTED .
Request HttpMessageConverter
( )
lombok.config lombok .
@Data
.
lombok.config
( )
,
, ,
…
3.
스프링 실전 가이드
.
,
*
.
.
.
.
.
.
.
.
(state) (behavior) .
.
.
.
' (what)' ' (how)'
.
.
.
.
.
Sample Code
' ' .
.
.
. .
' ' . .
.
, .
.
.
,
.
- What/Who
-
. What/Who .
What/Who
' What' ' Who'
. ' ' .
.
-
. Tell, Don't
.
.
.
.
.
.
.
Sample Code
,
.
.
(knowing)
*
*
*
.
" "
. .
.
.
-
.
.
,
.
.
.
, , .
.
. .
Sample Code
Sample Code
스프링 실전 가이드
4. SOLID
SOLID
SRP:
OCP:
LSP:
ISP:
DIP:
* … …
SOLID ?
: Open Close Principle
, .
.
스프링 실전 가이드
OCP
PamentController ShinhanCardPaymentService
. (OCP )
(
) .
API
. API .
API .
Request API
RequestBody API ,
.
shinhanCardNumber, number
Request .
Request API
API Request .
DIP ' .' . (
DIP .)
Request .
Request API -> OCP
if .
. if
. if .
XXXPaymentService
. PaymentController ,
, .
OCP
OCP
OCP
.
PaymentController CardPayment .
( ) ( )
( ) .
cardPaymentFactory .
,
.
스프링 실전 가이드
: Dependency Inversion Principle
.
.
.
.
.
.
.
스프링 실전 가이드
DIP
( ) ( )
( ) ( ) .
JSON , .
.
, PaymentRequest Dto CardType
.
.
if . if .
XXXPaymentService .
PaymentController ,  ,
. 
. DIP OCP SOLID
.
DIP
. .
( ) ( ) . ( ) (
) ( ) .
PaymentController PaymentService
cardPaymentFactory ShinhanCardPaymentService .
, PaymenrService
OCP .
스프링 실전 가이드
: Single Responsibility Principle
… ….
.
.
(Actor) ?
SRP
SRP
:
: Actor
Actor :
SRP
Actor
Actor pay
. Actor .
Actor Actor ( )
.
Actor
SRP( )
SRP( )
.
CardPaymentService payOverseas
.
?
payOverseas pay .
.
Actor .
( = Actor)
.
,
?
Actor . Actor
. .
, ,
SRP
.
.
SRP( )
.
CardPaymentService payOverseas
.
?
payOverseas pay .
SRP( )
PaymentService ,
.
.
: The Liskov Substitution Principle
.
Exception .
.
.
:
: Sample Code
:
Coupon calculateDiscountAmount() item getPrice()
Item , Item
SpecialItem
Coupon calculateDiscountAmount item
SpecialItem 0
:
instanceof LSP .
Item SpecialItem Item
.
instanceof SpecialItem .
SpecialItem Item
.
:
instanceof Item
. SpecialItem
. OCP
LSP Item .
.
:
:
Item SpecialItem
.
instanceof
.
Item .
OCP . LSP .
5.
?
?
…
Spring Boot Test
Bean Context .
.
Spring Boot Test
@SpringBootTest .
.
@SpringBootTest .
.
.
.
@RunWith JUnit
.
Spring Boot Test
Spring Boot Test
properties: {key=value} .
classes: .
@SpringBootConfiguration .
webEnvironment: . Mock
@ActiveProfiles("test")
@Transactional .
WebEnvironment.RANDOM_PORT,
DEFINED_PORT .
@Transactional rollback .
@WebMvcTest
@DataJpaTest
@DataJpaTest JPA .
, JPA , ,
.
.
@Entity JPA .
@DataJpaTest
@DataJpaTest
@AutoConfigureTestDatabase Replace.Any
.
@AutoConfigureTestDatabase(replace =
AutoConfigureTestDatabase.Replace.NONE) @ActiveProfiles("test")
.
@DataJpaTest .
.
@RestClientTest
@RestClientTest REST .
REST JSON .
@RestClientTest
@RestClientTest Bean .
@Rule @Before, @After
.
.
MockRestServiceServer REST .
RestTemplate .
.
rest_test() .
test.json ( .).
1. Mock Service = POJO
2. WebMvc Test
3. Spring Boot Test
Code
Code
Repository Test
Code
POJO
Code
스프링 실전 가이드
POJO
Test , Spring Boot
. Slice
.
POJO POJO
.
POJO
POJO .
.
.
POJO
Document
Matchers AssertJ .
assertThat(coupon.isUsed()) .
.
Controller
json
.
json .
Controller
스프링 실전 가이드
Import Set up
SQL given
when, then
.
.
.
. member insert
.
data.sql
.
Import Set up ->
Junit5 With Kotlin & Spring Boot 2.2
스프링 실전 가이드
스프링 실전 가이드
스프링 실전 가이드
Junit5 With Kotlin & Spring Boot 2.2
Junit5 With Kotlin & Spring Boot 2.2
Junit5 With Kotlin & Spring Boot 2.2
Junit5 With Kotlin & Spring Boot 2.2
* AssertJ BDD
BDDAssertion
.
스프링 실전 가이드
스프링 실전 가이드
6. Exception
Exception ?
.
.
.
.
Exception
.
.
.
.
.
.
UnCheckedException
.
:
. ( )
. ( )
Catch . ( )
Catch . ( )
Try Catch
Check Exception VS UnChecked Exception
Error .
Error …
Checked, Unchecked Exception
Checked = . (
, ) ->
Unchecked =
Runtime Exception
Check Exception VS UnChecked Exception
Error .
Error …
Checked, Unchecked Exception
Checked = . ( , ) ->
Unchecked =
Check Exception
JsonProcessingException IOException
Exception Checked Exception .
throws try catch
throw .
.
Unchecked Exception
.
Rollback
Rollback
(1) RuntimeException
yun member
rollback .
(2) IOException
wan rollback
commit .
member yun, wan
insert yun
rollback wan rollback
commit .
Checked Exception Rollback ?
Checked Exception Rollback ?
Checked Exception
.
.
.
Rollback
( 100%)
Checked Exception Rollback ?
.
…
(1)…
Checked Exception
.
SQLException
? + insert
RuntimeException
.
(1)…
Exception
Exception .
DuplicateEmailException (Unchecked Exception)
.
Checked Exception Unchecked Exception
. JPA
Checked Exception
RuntimeException .
Checked Exception
. throw
try catch .
.
Unckecked Exception
Checked Exception Unckecekd Exception
.
.
API Server Exception
Error Response
Error Response Error Response .
.
Map<Key, Value> .
.
Error Response
message : message .
status : http status code . header
.
errors : field, value, reason
. @Valid JSR 303: Bean
Validation .
errors null
[] . null
. null .
code :
Error Response
ErrorResponse . POJO
errorResponse.getXXX();
.
Exception ErrorResponse
. .
@ControllerAdvice
@ExceptionHandler
Error Response .
BusinessException
. .
Error Code
enum .
,
.
C001 enum
. Common
.
Business Exception
Business Exception Exception .
. Exception
.
Exception Business Exception .
Exception .
.
.
, ,
. Exception .
BusinessException
BusinessException InvalidValueException, EntityNotFoundExceptuon .
InvalidValueException : Excetion
,
EntityNotFoundException :
findById, findByCode
BusinessException
BusinessException .
BusinessException
.
handleBusinessException .
. BusinessException
.
.
.
.
. Bean Validation
. @ControllerAdvice .
.
Custom Validation (1)
Bean Validation
.
.
Custom Validation (1)
Custom Validation (1)
Custom Validation (2)
JSR 303 Validation . @NotNull,
@NotEmpty, @Email Validation .
, .
JSON account, card paymentMethod @NotNull,
@NotEmpty . ConstraintValidator .
ConstraintValidator
ConstraintValidator Validation ConstraintValidator
.
. Validation , ( ) .
ConstraintValidator
.
Interceptor .
ConstraintValidator (Interceptor ) .
ErrorResponse
- Error Response
ErrorReponse .
ConstraintValidator @Valid
MethodArgumentNotValidException
@ControllerAdvice
ErrorResponse
.
ErrorResponse
ConstraintValidator
ConstraintValidator
7.
스프링 실전 가이드
bRepository.save(B("B"))
aRepository.save(A("A")) Rollback .
CGLIB Proxy . (
Dynamic Proxy )
Proxy .
AOP
JPA Repository interface ?
@Transactional aRepository, bRepository .
Service
.
bRepository.save(B("B")) exception
bRepository.save(B("B")), aRepository.save(A("A")) rollback
.
Isolation Level
5
스프링 실전 가이드
스프링 실전 가이드
SQL 10 sleep
increase() RuntimeException
READ_UNCOMMITTED, READ_COMMITTED
스프링 실전 가이드
1 . (
)
2 (
READ_UNCOMMITTED 2
1
)
1 wake up Exception
2 15
Duty Read .
READ_COMMITTED
.
2 1
.
.
READ_COMMITED DB
.
/
.
1. 1 sleep ( )
2. 2 . ( READ_COMMITTED
.)
3. 1 .
Nonrepeatable Read
REPEATABLE_READ
1 , 2
 
.
REPEATABLE_READ .
REPEATABLE_READ
1. 1 sleep ( )
2. 2 1 .
REPEATABLE_READ DB
. /
.
1. 1
2. 2 insert
3. 1
( )
.
SERIALIZABLE .
SERIALIZABLE .
SERIALIZABLE
스프링 실전 가이드
: Lost Update
: Lost Update
T1, T2 .
T1 , T2 T1 T1
T2 .
T1 T2 T1 .
스프링 실전 가이드
.
.
.
DBMS .
.
.
.
. ->
. ->
1
2 ,
2 1
.
1
. 2
. 1
2 2
1 .
.
1.
2.
3.
.
4.
.
.
5.
.
DBMS
. DBMS for
update
.
JPA EntityManager LockModeType
find() ,
LockModeType.PESSIMISTIC_WRITE
.
JPA DBMS
, PESSIMISTIC_WRITE
for update
.
1. 1: A
2. 2: B
3. 1: B
4. 2 : A
. 1
2 .
,
. .
. JPA
.
JPA javax.persistence.lock.timeout .
. (DBMS . )
Isolcation Level REPEATABLE_READ DB
.
1
2
.
JPA REPEATABLE_READ
.
In JPA
JPA . 1
A , B . Commit
.
REPEATABLE_READ ( )
1 REPEATABLE_READ .
In JPA
A B
. second
lost updates problem . 
.   .  3 .
1. : A B .
2. : A B .
3. : A B .
1. .
2. .
.
3.
.
4. 1
.
.
.
,
.
,
.
DBMS
.
.
1
, .
.
1 . ,
.
1. 1
2. 2
3. 1 ( 2
), 6 .
4. 2 ,
6 2
JPA .
@Version
.
JPA UPDATE
@Version
. 10 UP-
DATE
.
. Repository
.
JPA
.
Flow
Application Event
Application Event
( )
( , )
Application Event
OrderService
. , OrderService .
Application Event .
Application Event
- Application Event Subscriber
.
OrderService
.
Application Event
(Item) , event .
.
.
.
JPA
JPA Setter
, ,
setter .
setter
.
Setter .
Account updateMyAccount
.
.
MyAccountReq .
DTO
.
Setter .
setter
.
.
setter
setter
.
JPA
.
.
1:1 OneToOne .
.
?
OneToOne
.
JPA
( , , )
.
?
order coupon_id
SQL .
coupon
coupon order order INSERT
SQL , coupon order_id
2 .
Order : INSERT SQL
OneToOne
.
order coupon_id
coupon
order_id .
OneToMany
.
.
Order :
OneToOne ,
. OneToMany
Many . OneToMany
OneToMany .
OneToOne .
<->
OneToOne . ( .
)
.
@JoinColumn nullable false . NOT
NULL .
JPA DDL Spec .
NOT NULL . JPA SQL
.
NOT NULL .
. .
.
applyCoupon .
order
.
order 1,000
.
null .
.
.
1:N .
1
OneToMany
Delivery DeliveryLog
PENDDING
.
Delivery
CaseCade PERSIST Delivery
DeliveryLog .
CaseCade PERSIST
,
insert query .
Delivery
JPA
.
.
Delivery DeliveryLog
.
(orphanRemoval) : DeliveryLog
delivery, deliverylog
Delivery .
delete SQL delivery_log
delivery .
orphanRemoval
.
(orphanRemoval) : Delivery
DeliveryLog clear() DeliveryLog
Delivery DeliveryLog .
(orphanRemoval) :
Embedded
String Email . .
. .
Email String Email
.
String Account ,
.
Account . Account
.
.
.
Email Email . Email
. . .
(Rich Object)
Money , , .
.
JPA N+1
ERD
N+1
N+1
N+1
  Member N+1 .
N+1
N+1
JPQL . findById()
JPQL SQL . .
1. JPQL .
2. .
3. ( ) .
N+1
JPQL JPA SQL . JPQL ,
JPQL SQL .
: Batch Size
@BatchSize(size = 5) size . size where in
size where in .
spring.jpa.properties.hibernate.default_batch_fetch_size=1000 properties
: Fetch
fetch . Fetch
.
Fetch
.
Fetch
SQL . JPQL
select .
Lazy N+1
.
Fetch 

Fetch API .
limit offset .
API
.
FULL Scan
limit .
Full Sacn
.
API
.
Fetch 

.
.  annot simultaneously fetch
multiple bag  . List -> Set .
Fetch 

.
.  annot simultaneously fetch
multiple bag  . List -> Set .
스프링 실전 가이드
Properties
스프링 실전 가이드
key
properties value .
amount 100 int amount
. .
email email-address getProperty()
email-address .
1 runtime
NullPointException .
: ConfigurationProperties
스프링 실전 가이드
: ConfigurationProperties
Immutable Properties
@ConstructorBinding data class
.
2.2.1
@ConfigurationProperties scanning
disabled
@EnableConfigurationProperties
.
END

More Related Content

PPT
PDF
Frontin like-a-backer
KEY
IoC with PHP
PDF
How to build customizable multitenant web applications - PHPBNL11
ODP
Dependency Injection, Zend Framework and Symfony Container
PDF
Practical Protocol-Oriented-Programming
PDF
droidparts
PDF
Android programming -_pushing_the_limits
Frontin like-a-backer
IoC with PHP
How to build customizable multitenant web applications - PHPBNL11
Dependency Injection, Zend Framework and Symfony Container
Practical Protocol-Oriented-Programming
droidparts
Android programming -_pushing_the_limits

What's hot (19)

PDF
Web注入+http漏洞等描述
PDF
DataFX 8 (JavaOne 2014)
PDF
Connect.Tech- Level Up Your Game With TravisCI
PDF
Java Web Programming [8/9] : JSF and AJAX
PDF
Workshop 26: React Native - The Native Side
PDF
Connect.Tech- Enhancing Your Workflow With Xcode Source Editor Extensions
PDF
Scala ActiveRecord
PPTX
SCULPT! YOUR! TESTS!
PDF
Explaination of angular
PDF
Connect.Tech- Swift Memory Management
PDF
Building an app with Google's new suites
PPTX
PPT
2 introduction toentitybeans
PDF
Java Web Programming [5/9] : EL, JSTL and Custom Tags
PDF
Jetpack, with new features in 2021 GDG Georgetown IO Extended
ODP
To inject or not to inject: CDI is the question
PDF
Real World Dependency Injection - IPC11 Spring Edition
PPTX
Javascript Testing with Jasmine 101
PPTX
Extending burp with python
Web注入+http漏洞等描述
DataFX 8 (JavaOne 2014)
Connect.Tech- Level Up Your Game With TravisCI
Java Web Programming [8/9] : JSF and AJAX
Workshop 26: React Native - The Native Side
Connect.Tech- Enhancing Your Workflow With Xcode Source Editor Extensions
Scala ActiveRecord
SCULPT! YOUR! TESTS!
Explaination of angular
Connect.Tech- Swift Memory Management
Building an app with Google's new suites
2 introduction toentitybeans
Java Web Programming [5/9] : EL, JSTL and Custom Tags
Jetpack, with new features in 2021 GDG Georgetown IO Extended
To inject or not to inject: CDI is the question
Real World Dependency Injection - IPC11 Spring Edition
Javascript Testing with Jasmine 101
Extending burp with python
Ad

Similar to 스프링 실전 가이드 (20)

ODP
Javaee6 Overview
PDF
Design Summit - Rails 4 Migration - Aaron Patterson
ODP
What's new in Java EE 6
PDF
A Lifecycle Of Code Under Test by Robert Fornal
PDF
Introduction to Protractor
KEY
Aprendendo solid com exemplos
ODP
Performance Optimization of Rails Applications
PPTX
Extending burp with python
PPTX
Extending burp with python
PPTX
Extending burp with python
PPTX
Extending burp with python
PPTX
Extending burp with python
PPTX
Extending burp with python
PPTX
Scientific calcultor-Java
PDF
SproutCore and the Future of Web Apps
PPTX
Designing REST API automation tests in Kotlin
PDF
Cleaning your architecture with android architecture components
PPTX
Java final project of scientific calcultor
PDF
Icpc2010 bettenburg
PPTX
Behavior driven oop
Javaee6 Overview
Design Summit - Rails 4 Migration - Aaron Patterson
What's new in Java EE 6
A Lifecycle Of Code Under Test by Robert Fornal
Introduction to Protractor
Aprendendo solid com exemplos
Performance Optimization of Rails Applications
Extending burp with python
Extending burp with python
Extending burp with python
Extending burp with python
Extending burp with python
Extending burp with python
Scientific calcultor-Java
SproutCore and the Future of Web Apps
Designing REST API automation tests in Kotlin
Cleaning your architecture with android architecture components
Java final project of scientific calcultor
Icpc2010 bettenburg
Behavior driven oop
Ad

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
cuic standard and advanced reporting.pdf
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Programs and apps: productivity, graphics, security and other tools
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

스프링 실전 가이드