일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 자바 백준 팩토리얼 개수
- 자바 1676
- 백준 특정한 최단 경로
- 백준 연결요소 자바
- java 1509
- java 파티
- Spring ipfs
- 전략 패턴이란
- kotiln const val
- nodejs rabbitmq
- spring mongodb switch
- 안정해시
- rabbitmq 싱글톤
- spring mongodb
- java 팩토리얼 개수
- Java Call By Refernce
- 백준 2252 줄세우기
- kotiln const
- ipfs singletone
- go
- kotiln functional interface
- 백준 1504 java
- java 1238
- spring mongoTemplate
- javav 1676
- ipfs bean
- spring mongoTemplate switch
- mongodb lookup
- java 백준 1509
- 익명 객체 @transactional
Archives
- Today
- Total
공부 흔적남기기
Spring 익명 객체 함수에 @Transactional을 사용하면 어떻게 될까? 본문
728x90
반응형
다음과 같이 구성되어있다고 가정하자
익명객체를 만들고 익명객체의 함수에는 @Transactional이 붙어있으며 해당 함수는 Transcation이 필요한 함수를 호출한다.
@FunctionalInterface
interface TempHandleInterface {
fun handle(something: String): String
}
import jakarta.annotation.PostConstruct
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.UUID
@Service
class TempClass(
private val tempService: TempService,
) {
private val tempHandlers: MutableMap<String, TempHandleInterface> = mutableMapOf();
@PostConstruct
fun init(){
tempHandlers["secretHandler"] = object : TempHandleInterface{
@Transactional
override fun handle(something: String): String {
return tempService.saveNeedToTransaction(UUID.randomUUID().toString() + something + "secret")
}
}
}
}
import org.springframework.stereotype.Service
@Service
class TempService {
fun saveNeedToTransaction(something : String): String {
//need to save something on transcation
return "Success"
}
}
과연 saveNeedToTransaction는 Transaction이 적용될까???
적용되지 않는다 Spring AOP는 주로 어노테이션기반으로 작동하는데 이때 Spring AOP가 프록시를 만들어주는 대상은 Spring Bean에서 관리되는 되어야한다, 하지만 익명객체는 Spring Bean으로 등록되어 있지 않기 떄문에 @Transcational을 포함한 Spring Aop 기반 어노테이션이 동작하지 않는다.
따라서 호출되는 saveNeedToTransaction에 @Transactional을 붙여서 사용해야한다
728x90
반응형
'web study > Spring' 카테고리의 다른 글
Spring mongodb id mapping bug (0) | 2024.01.05 |
---|---|
Spring BeanPostProcessor(빈 후처리기) (0) | 2023.12.22 |
프록시 팩토리 | Advice | Pointcut (0) | 2023.12.20 |
Spring Scheduler SchedLock MongoDB 설명 및 구현 (0) | 2023.12.01 |
Spring mongodb mongoTemplate project stage에서 switch 사용하기 (0) | 2023.11.28 |