InitializingBean
은 afterPropertiesSet()
메서드로 초기화를 지원한다. DisposableBean
은 destroy()
메서드로 소멸을 지원한다.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+"messag = "+message);
}
public void disconnect() {
System.out.println("close : " + url);
}
// 4. 초기화 콜백
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("생성자 호출, url = " + url);
connect();
call("초기화 연결 메시지");
}
// 5. 소멸 전 콜백
@Override
public void destroy() throws Exception {
disconnect();
}
}
Test
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() {
// 1. 스프링 컨테이너 생성
// 2. 빈 생성
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
// 3. 의존 관계 주입
NetworkClient networkClient = ac.getBean(NetworkClient.class);
// 6. 종료
ac.close();
}
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
@Bean(initMethod = "init", destroyMethod = "close")
처럼 초기화, 소멸 메서드를 지 정할 수 있다close
, shutdown
이라는 이름의 종료 메서드를 사용한다. destroyMethod
는 기본값이 (inferred)
(추론)으로 등록되어 있다. 이 추론 기능은 close
, shutdown
라는 이름의 메서드를 자동으로 호출해준다. 이름 그대로 종료 메서드를 추 론해서 호출해준다. destroyMethod=""
처럼 빈 공백을 지정하면 된다.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+"messag = "+message);
}
public void disconnect() {
System.out.println("close : " + url);
}
// 4. 초기화 콜백
public void init() throws Exception {
System.out.println("생성자 호출, url = " + url);
connect();
call("초기화 연결 메시지");
}
// 5. 소멸 전 콜백
public void close() throws Exception {
disconnect();
}
}
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() {
// 1. 스프링 컨테이너 생성
// 2. 빈 생성
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
// 3. 의존 관계 주입
NetworkClient networkClient = ac.getBean(NetworkClient.class);
// 6. 종료
ac.close();
}
@Configuration
static class LifeCycleConfig {
@Bean(initMethod = "init", destroyMethod = "close")
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
javax.annotation.PostConstruct
이다. 스프링에 종속적인 기술이 아니라 JSR-250 라는 자바 표준이다. @Bean
의 initMethod
, destroyMethod
사용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);
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+"messag = "+message);
}
public void disconnect() {
System.out.println("close : " + url);
}
// 4. 초기화 콜백
@PostConstruct
public void init() throws Exception {
System.out.println("생성자 호출, url = " + url);
connect();
call("초기화 연결 메시지");
}
// 5. 소멸 전 콜백
@PreDestroy
public void close() throws Exception {
disconnect();
}
}
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() {
// 1. 스프링 컨테이너 생성
// 2. 빈 생성
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
// 3. 의존 관계 주입
NetworkClient networkClient = ac.getBean(NetworkClient.class);
// 6. 종료
ac.close();
}
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}