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

스프링 실전 가이드