sggnology
하늘속에서IT
sggnology
전체 방문자
오늘
어제
  • 분류 전체보기 (83)
    • Algorithm (31)
      • Programmers (27)
      • Baekjoon (4)
    • WIKI (4)
      • VirtualBox (1)
      • Power Toys (1)
    • NodeJS (4)
      • nvm (1)
      • React (1)
      • Vue (1)
    • Dev Language (3)
      • Java (2)
      • Kotlin (1)
    • Spring Boot (17)
      • Gradle (1)
      • JPA (3)
    • DB (4)
      • MariaDB (3)
      • Redis (0)
    • Android (6)
      • Debug (3)
    • Nginx (3)
      • Debug (1)
    • Intellij (0)
    • Network (1)
    • Git (2)
      • GitHub (2)
    • Chrome Extension (0)
    • ETC (5)
      • Monitoring (2)
    • Linux (1)
      • WSL (1)
    • Visual Studio (1)
    • Side Project (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 오블완
  • 알고리즘
  • 안드로이드 스튜디오
  • 레벨3
  • spring boot
  • 백준
  • DB
  • docker
  • 프로그래머스
  • kotlin
  • 티스토리챌린지
  • java
  • 레벨2
  • Android Studio
  • 고득점 Kit
  • 고득점KIT
  • nginx
  • 연습문제
  • mariadb
  • JPA

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
sggnology

하늘속에서IT

[Spring Boot] Spring 에서 Redis 를 사용할 때 ConnectionFactory 를 왜 Bean 으로 등록해서 사용해야 할까?(feat. Kotlin, Lettuce, LettuceConnectionFactory was not initialized through afterPropertiesSet())
Spring Boot

[Spring Boot] Spring 에서 Redis 를 사용할 때 ConnectionFactory 를 왜 Bean 으로 등록해서 사용해야 할까?(feat. Kotlin, Lettuce, LettuceConnectionFactory was not initialized through afterPropertiesSet())

2023. 6. 10. 22:39
728x90

발단

RedisTemplate 의 connectionFactory 를 Bean 으로 등록된 RedisConnectionFactory 로 사용하는 예제가 많은데 왜 그래야 하는지 설명이 없어 궁금하여 찾아보게 되었다.
(Bean 으로 등록하여 사용하지 않으면 `LettuceConnectionFactory was not initialized through afterPropertiesSet()` 오류가 발생한다.)

설명

  1. Spring 에서 Redis 서버와의 연결을 위해 getConnection 메서드를 호출한다.
  2. getConnection 메서드 내부에는 assertInitialized 검증 메서드를 호출한다.
  3. assertIntialized 는 initalized 변수를 통해 초기화 여부를 판단하는데, 해당 변수의 값 세팅을 afterPropertiesSet 메서드가 처리한다.
  4. afterPropertiesSet 메서드는 InitializingBean 의 구현체가 가지는 메서드로써 bean 이 초기화 될 때 호출된다.
  5. 즉, redis template 의 connectionFactory 를 등록시킬 때 Bean 으로 등록된 factory 를 등록해야지 검증 과정을 통과할 수 있게 된다.

 

상세 설명

 

Bean 으로 등록하지 않은 connection factory 사용시 오류 발생

  • getConnection 에서 문제가 발생한걸 확인할 수 있다.

 

GetConnection

  • assertInitialized() 메서드 호출을 통해 검증 과정을 거친다.

 

assertInitialized()

  • initialized 변수로 assert 를 통해 확인한다.

 

afterPropertiesSet() 메서드(initialized 변수 초기화 시점)

  • Bean 으로 등록되면 afterPropertiesSet 메서드를 Bean 초기화 시점에 호출하게 될 것이고, 초기화 변수가 세팅되게 된다.

 

 

 

728x90

'Spring Boot' 카테고리의 다른 글

[Spring Boot] Access denied for user 'root'@'localhost'(using password: yes)  (1) 2024.04.24
[Spring Boot] @ModelAttribute 로 맵핑하려는 대상의 depth 가 2 이상일 때 요청하였는데 오류가 난다면?(feat. kotlin)  (0) 2023.10.16
[Spring Boot] custom error page 적용(feat. 예제, ErrorController)  (0) 2023.05.26
[Spring Boot] JWT 라이브러리를 의존성 추가했음에도 빌드 오류가 나는건 왜일까?(feat. `io.jsonwebtoken:jjwt-api`)  (2) 2023.05.25
[Spring Boot] s3 aws-java-sdk(v 1.1) 을 사용하는 개발환경에서 SSL 인증서와 무관하게 통신하는 방법(feat. ValidatorException: PKIX path building failed)  (0) 2023.05.23
    'Spring Boot' 카테고리의 다른 글
    • [Spring Boot] Access denied for user 'root'@'localhost'(using password: yes)
    • [Spring Boot] @ModelAttribute 로 맵핑하려는 대상의 depth 가 2 이상일 때 요청하였는데 오류가 난다면?(feat. kotlin)
    • [Spring Boot] custom error page 적용(feat. 예제, ErrorController)
    • [Spring Boot] JWT 라이브러리를 의존성 추가했음에도 빌드 오류가 나는건 왜일까?(feat. `io.jsonwebtoken:jjwt-api`)
    sggnology
    sggnology
    하늘은 파란색이니까 내 삶도 파란색이길 ㅎㅎ

    티스토리툴바