ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • SPRING_06_ SpringBoot 로그 분석
    4.Spring/4.1 이론정리 2022. 9. 20. 11:17

    Test 코드

    package hello.springtx.apply;
    
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.context.TestConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.event.EventListener;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.transaction.support.TransactionSynchronizationManager;
    
    import javax.annotation.PostConstruct;
    
    @SpringBootTest
    public class initTxTest {
    
        @Autowired Hello hello;
    
        @Test
        void go(){
            //초기화 코드는 스프링이 초기화 시점에 호출.
    
        }
        @TestConfiguration
        static class InitTxTextConfig{
            @Bean
            Hello hello(){
                return new Hello();
            }
        }
        @Slf4j
        static class Hello{
            @PostConstruct
            @Transactional
            public void initV1(){
                boolean isActive = TransactionSynchronizationManager.isActualTransactionActive();
                log.info("Hello init @PostConstruct tx Active={}",isActive);
            }
            @EventListener(ApplicationReadyEvent.class) //스프링 컨테이너가 다 뜨면 호출
            @Transactional
            public void initV2(){
                boolean isActive = TransactionSynchronizationManager.isActualTransactionActive();
                log.info("Hello init2 @EventListener tx active={}",isActive);
            }
        }
    
    }

    INFO 17916 ---  [main] hello.springtx.apply.initTxTest$Hello    : Hello init @PostConstructor tx active=false

    =>initV1메소드는 @PostConstruct 애노테이션이 있기 때문에 생성자 주입전, 스프링 컨테이너가 뜨기 전에 생성됨. -> 트랜잭션은 스프링 컨테이너가 뜬 후 AOP가 적용되는 시점에 트랜잭션이 적용되기 때문에 트랜잭션 활성화가 되지 않음
    INFO 17916 ---  [main]hello.springtx.apply.initTxTest   : Started initTxTest in 2.392 seconds (JVM running for 3.246)

    => 스프링 컨테이너가 완료가 되어서 뜬 상황  EventListener(ApplicationReadyEvent.class) 호출
    TRACE 17916 ---  [main] o.s.t.i.TransactionInterceptor : Getting transaction for [hello.springtx.apply.initTxTest$Hello.initV2]

    => initV2 메소드의 트랜잭션 적용 로그

    INFO 17916 ---  [main] hello.springtx.apply.initTxTest$Hello    : Hello init @PostConstructor tx active=true

    => hello.initV2 메소드에는 EventListener(ApplicationReadyEvent.class) 스프링 컨테이너가 뜬 후에 프록시 객체를 생성하여 해당 매소드가 호출되므로 트랜잭션 실행 중인 상태임
    TRACE 17916 ---  [main] o.s.t.i.TransactionInterceptor           : Completing transaction for [hello.springtx.apply.initTxTest$Hello.initV2]

    => 메서드가 끝나면 트랜잭션 실행 종료

    '4.Spring > 4.1 이론정리' 카테고리의 다른 글

    SPRING_05_ mybatis-config.xml 설정  (0) 2022.06.14
    SPRING_04_ 기타 xml 설정(1)  (0) 2022.06.14
    SPRING_03_servlet-context.xml 설정  (0) 2022.06.14
    SPRING_02_ root-context.xml 설정  (0) 2022.06.14
    SPRING_01_ web.xml 설정  (0) 2022.06.14
Designed by Tistory.