Skip to content

Commit ae49bfa

Browse files
committed
annotation initialization
1 parent efa0966 commit ae49bfa

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

복습/src/test/java/jpa/demo/lifecycle/BeanLifeCycleTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ public void lifeCycleTest() {
2020

2121
@Configuration
2222
static class LifeCycleConfig{
23-
// 메서드 이름을 자유롭게 작성 가 + 외부 라이브러리의 초기화&종료 메서드 또한 호출 가능
24-
// 외부라이브러리의 초기화&종료 메서드는 대부분 추론 가능 (이름 : close or shutdown) -> 설정해주지 않아도 된다.
25-
@Bean(initMethod = "init", destroyMethod = "close")
23+
24+
@Bean
2625
public NetworkClient networkClient() {
2726
NetworkClient networkClient = new NetworkClient();
2827
networkClient.setUrl("http://spring-dev.com");

복습/src/test/java/jpa/demo/lifecycle/NetworkClient.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jpa.demo.lifecycle;
22

3+
import jakarta.annotation.PostConstruct;
4+
import jakarta.annotation.PreDestroy;
35
import org.springframework.beans.factory.DisposableBean;
46
import org.springframework.beans.factory.InitializingBean;
57

@@ -32,13 +34,17 @@ public void disconnect() {
3234

3335

3436
// 의존관계 주입이 완료되고 -> 실행됨
37+
// 최신 스프링부트에서 적극적으로 권장하는 방식
38+
// 단, 외부라이브러리의 초기화&종료 메서드 호출 시, 사용하지 못함. 수동방식으로 사용해야한다.
39+
@PostConstruct
3540
public void init() {
3641
System.out.println("init");
3742
connect();
3843
call("초기화 연결 메시지");
3944
}
4045

4146
// 빈 종료되기 이전에 -> 실행됨
47+
@PreDestroy
4248
public void close() {
4349
System.out.println("close");
4450
disconnect();

0 commit comments

Comments
 (0)