일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 접속 url 출력
- 스프링 부트 배너 설정
- 프로그래머스 공 던지기 게임
- 비전공자 정보처리기사 필기
- spring boot 배너 설정
- 배열 순환 자바
- 배열 순환
- 정처기 필기 벼락치기
- 스프링 부트 프로젝트 세팅
- string과 stringbuilder 성능 최적화
- 비전공자 정처기 필기
- cursor 설치
- 티스토리챌린지
- 소인수분해 구하는 공식
- 경우의 수 자바
- 프로그래머스 문자열 정렬하기(1)
- 프로그래머스
- 배열 순환 문제 공식
- 정보처리기사 필기 벼락치기
- 정보처리기사 필기
- cursor 우클릭 메뉴
- 펙토리얼
- 자바 팩토리얼
- 스프링부트 의존성 설정
- 숨어있는 숫자의 덧셈 (1) 자바
- 피그마 썸네일
- 자바 소인수분해
- 오블완
- 자바 합성수 찾기
- 왓챠피디아 클론 코딩
- Today
- Total
여름 언덕에서 배운 것
[스프링기본1]빈 생명주기 콜백 본문
스프링 핵심 원리 - 기본편 - 인프런 | 강의
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다., 스프링 핵심 원리를 이해하고, 성장하는 백엔드 개발자가 되어보세요! 📢
www.inflearn.com
빈 생명주기 콜백
애플리케이션이 시작할 때는 필요한 연결들(데이터베이스나 네트워크)을 미리 만들어 놓습니다. 그리고 애플리케이션을 종료할 때는 그 연결들을 다시 닫습니다. 이렇게 미리 연결을 준비하고 종료하는 작업을 '빈의 초기화와 종료 작업'이라고 합니다
package hello.core.lifecycle;
public class NetworkClient {
private String url;
public NetworkClient(){
System.out.println("생성자 호출 , url = " + url);
connect();
call("초기화 연결 메시지");
}
public void setUrl(String url) {
this.url = url;
}
// 서비스를 시작할 때 호출하는 메서드
public void connect(){
System.out.println("connect = " + url);
}
public void call(String message) {
System.out.println("call: " + url + " message = " + message);
}
//서비스 종료시 호출
public void disconnect() {
System.out.println("close: " + url);
}
}
package hello.core.lifecycle;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class BeanLifeCycleTest {
@Test
public void lifeCycleTest(){
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = ac.getBean(NetworkClient.class);
ac.close(); //스프링 컨테이너를 종료, ConfigurableApplicationContext(인터페이스) 필요
}
@Configuration //스프링 IoC 컨테이너에게 해당 클래스가 빈(bean) 설정 정보를 가지고 있음을 알립니다.
static class LifeCycleConfig{
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
null 이네~??
생성자 부분을 보면 url 정보 없이 connect가 호출되는 것을 확인할 수 있다.
너무 당연한 이야기이지만 객체를 생성하는 단계에는 url이 없고,
객체를 생성한 다음에 외부에서 수정자 주입을 통해서 setUrl() 이 호출되어야 url이 존재하게 된다
**생성자 주입은 예외입니다
생성자는 객체를 만들 때 이미 파라미터로 들어와 있습니다.
스프링 빈은 간단하게 다음과 같은 라이프사이클을 가진다.
객체 생성 -> 의존관계 주입
스프링 빈은 객체를 생성하고, 의존관계 주입이 다 끝난 다음에야 필요한 데이터를 사용할 수 있는 준비가 완료된다.
따라서 초기화 작업은 의존관계 주입이 모두 완료되고 난 다음에 호출해야 한다. 그런데 개발자가 의존관계 주입이 모두
완료된 시점을 어떻게 알 수 있을까?
스프링은 의존관계 주입이 완료되면 스프링 빈에게 콜백 메서드를 통해서 초기화 시점을 알려주는 다양한 기능을 제공
한다. 또한 스프링은 스프링 컨테이너가 종료되기 직전에 소멸 콜백을 준다.
따라서 안전하게 종료 작업을 진행할 수 있다.
스프링 빈의 이벤트 라이프사이클
스프링 컨테이너 생성, 스프링 빈 생성, 의존관계 주입(특히 setter 인젝션, 수정자 인젝션,필드인젝션) ,초기화 콜백 사용 ,소멸전 콜백 ,스프링 종료
생성자는 스프링 빈 생성할 때 어느정도 의존관계가 주입된다.
초기화 콜백: 빈이 생성되고, 빈의 의존관계 주입이 완료된 후 호출
소멸전 콜백: 빈이 소멸되기 직전에 호출
객체 생성과 초기화를 따로 하자 !
예를 들어, 우리가 외부 서비스와의 연결을 관리하는 `ExternalServiceConnector`라는 클래스를 가지고 있다고 생각해봅시다.
### 좋지 않은 예시:
생성자에서 연결까지 모두 처리하는 경우:
public class ExternalServiceConnector {
private Connection connection;
public ExternalServiceConnector(String endpoint) {
// 객체 생성
this.connection = new Connection();
// 무거운 연결 초기화 작업
this.connection.connectTo(endpoint);
}
}
위 예시에서는 `ExternalServiceConnector` 객체가 생성될 때 동시에 연결까지 모두 이루어집니다.
이렇게 되면, 객체 생성 시점에 무거운 작업이 이루어져 부하가 발생할 수 있고,
오류 관리나 유지보수가 어려워질 수 있습니다.
### 좋은 예시:
생성과 초기화를 분리한 경우:
public class ExternalServiceConnector {
private Connection connection;
public ExternalServiceConnector() {
// 객체 생성만 처리
this.connection = new Connection();
}
public void initialize(String endpoint) {
// 무거운 연결 초기화 작업
this.connection.connectTo(endpoint);
}
}
사용 방법:
ExternalServiceConnector connector = new ExternalServiceConnector();
connector.initialize("http://example.com");
위 예시에서는 객체 생성과 초기화 작업을 명확하게 분리하였습니다. 이렇게 구성하면 객체 생성 시점에 발생할 수 있는 부하나 오류를 줄이고, 필요한 시점에만 초기화 작업을 수행할 수 있게 됩니다. 게다가 초기화 작업에 문제가 생겼을 때 오류를 파악하기도 더 쉽습니다.
이렇게 생성과 초기화를 분리하는 방식은 유지보수와 오류 관리에 더 유리하며, 코드의 명확성과 가독성도 향상시킵니다.
인터페이스 InitializingBean, DisposableBean
package hello.core.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class NetworkClient implements InitializingBean, DisposableBean {
private String url;
public NetworkClient(){
System.out.println("생성자 호출 , url = " + url);
connect();
call("초기화 연결 메시지");
}
public void setUrl(String url) {
this.url = url;
}
// 서비스를 시작할 때 호출하는 메서드
public void connect(){
System.out.println("connect = " + url);
}
public void call(String message) {
System.out.println("call: " + url + " message = " + message);
}
//서비스 종료시 호출
public void disconnect() {
System.out.println("close: " + url);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("NetworkClient.afterPropertiesSet");
connect();
call("초기화 연결 메세지");
}
@Override
public void destroy() throws Exception {
System.out.println("NetworkClient.destroy");
disconnect();
}
}
InitializingBean 은 afterPropertiesSet() 메서드로 초기화를 지원한다.
DisposableBean 은 destroy() 메서드로 소멸을 지원한다
생성자 호출 할 때는 당연히 url 이 null
그다음 url이 연결이 되고
스프링컨테이너가 종료되자
destroy() 호출된 걸 볼 수 있다.
참고: 인터페이스를 사용하는 초기화, 종료 방법은 스프링 초창기에 나온 방법들이고, 지금은 다음의 더 나은 방법
들이 있어서 거의 사용하지 않는다.
빈 등록 초기화, 소멸 메서드
설정 정보에 @Bean(initMethod = "init", destroyMethod = "close") 처럼 초기화, 소멸 메서드를 지정할 수 있다
스프링 빈이 스프링 코드에 의존하지 않는다.
코드가 아니라 설정 정보를 사용하기 때문에 코드를 고칠 수 없는 외부 라이브러리에도 초기화, 종료 메서드를 적
용할 수 있다.
package hello.core.lifecycle;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class BeanLifeCycleTest {
@Test
public void lifeCycleTest(){
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = ac.getBean(NetworkClient.class);
ac.close(); //스프링 컨테이너를 종료, ConfigurableApplicationContext(인터페이스) 필요
}
@Configuration //스프링 IoC 컨테이너에게 해당 클래스가 빈(bean) 설정 정보를 가지고 있음을 알립니다.
static class LifeCycleConfig{
@Bean(initMethod = "init", destroyMethod = "close")
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
package hello.core.lifecycle;
public class NetworkClient {
//implements InitializingBean, DisposableBean 인터페이스 빈 소멸과 생성
private String url;
public NetworkClient(){
System.out.println("생성자 호출 , url = " + url);
connect();
call("초기화 연결 메시지");
}
public void setUrl(String url) {
this.url = url;
}
// 서비스를 시작할 때 호출하는 메서드
public void connect(){
System.out.println("connect = " + url);
}
public void call(String message) {
System.out.println("call: " + url + " message = " + message);
}
//서비스 종료시 호출
public void disconnect() {
System.out.println("close: " + url);
}
public void init() {
System.out.println("NetworkClient.init");
connect();
call("초기화 연결 메시지");
}
public void close() {
System.out.println("NetworkClient.close");
disconnect();
}
}
종료 메서드 추론 (@Bean 일 때만)
@Bean의 destroyMethod 속성에는 아주 특별한 기능이 있다.
라이브러리는 대부분 close , shutdown 이라는 이름의 종료 메서드를 사용한다.
@Bean의 destroyMethod 는 기본값이 (inferred) (추론)으로 등록되어 있다.
이 추론 기능은 close , shutdown 라는 이름의 메서드를 자동으로 호출해준다. 이름 그대로 종료 메서드를 추
론해서 호출해준다.
따라서 직접 스프링 빈으로 등록하면 종료 메서드는 따로 적어주지 않아도 잘 동작한다.
추론 기능을 사용하기 싫으면 destroyMethod="" 처럼 빈 공백을 지정하면 된다.
애노테이션 @PostConstruct, @PreDestroy
결론은 이 방식을 쓰면 된다!!
최신 스프링에서 가장 권장하는 방법이다.
package hello.core.lifecycle;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class BeanLifeCycleTest {
@Test
public void lifeCycleTest(){
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = ac.getBean(NetworkClient.class);
ac.close(); //스프링 컨테이너를 종료, ConfigurableApplicationContext(인터페이스) 필요
}
@Configuration //스프링 IoC 컨테이너에게 해당 클래스가 빈(bean) 설정 정보를 가지고 있음을 알립니다.
static class LifeCycleConfig{
@Bean//(initMethod = "init", destroyMethod = "close")
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
package hello.core.lifecycle;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
public class NetworkClient {
private String url;
public NetworkClient() {
System.out.println("생성자 호출, url = " + url);
}
public void setUrl(String url) {
this.url = url; }
//서비스 시작시 호출
public void connect() {
System.out.println("connect: " + url);
}
public void call(String message) {
System.out.println("call: " + url + " message = " + message);
}
//서비스 종료시 호출
public void disConnect() {
System.out.println("close + " + url);
}
@PostConstruct
public void init() {
System.out.println("NetworkClient.init");
connect();
call("초기화 연결 메시지");
}
@PreDestroy
public void close() {
System.out.println("NetworkClient.close");
disConnect();
}
}
'가랑비에 옷 젖는 줄 모른다 💻 > 스프링' 카테고리의 다른 글
스프링 MVC 1편 - 백엔드 웹 개발 핵심 기술 (0) | 2023.11.29 |
---|---|
[스프링기본1] 빈 스코프 (0) | 2023.11.02 |
[스프링기본1]섹션7 - 애노테이션 직접 만들기 , 조회한 빈이 모두 필요할 때, List, Map , 올바른 실무 운영 기준 (0) | 2023.10.22 |
[스프링기본1] 섹션 7 -@Autowired 필드 명, @Qualifier, @Primary (1) | 2023.10.21 |
[스프링기본1]섹션7 - 조회 빈이 2개 이상 (0) | 2023.10.20 |