//์์ ์ฝ๋
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.ApplicationContext;
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 = newAnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = ac.getBean(NetworkClient.class);
ac.close(); //์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์ข
๋ฃ, ConfigurableApplicationContext ํ์
}
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}
ํ ์คํธ ์ฝ๋๋ฅผ ์คํํ๋ฉด ๊ฐ์ฒด ์์ฑ ๋จ๊ณ์๋ ์ค์ ๋ url์ด ์๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ๊ฒ์ด๋ค. ์ฝ๋์์ setUrl(์์ ์ ์ฃผ์ )์ ํตํด์ผ๋ง url์ด ๋ง๋ค์ด์ง๋ค.
์์ฑ์ ํธ์ถ, url = null
connect: null
call: null message = ์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง
๐๐ป์คํ๋ง ๋น์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ์์กด๊ด๊ณ ์ฃผ์
์ด ๋ค ๋๋ ๋ค์์์ผ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ค๋น๊ฐ ์๋ฃ๋๋คโ๏ธ ๋ฐ๋ผ์ ์ด๊ธฐํ ์์
์ ์์กด๊ด๊ณ ์ฃผ์
์ด ๋ชจ๋ ์๋ฃ๋๊ณ ๋ ๋ค์์ ํธ์ถํด์ผ ํ๋ค. ๊ทธ๋ฐ๋ฐ ๊ฐ๋ฐ์๊ฐ ์์กด๊ด๊ณ ์ฃผ์
์ด ๋ชจ๋ ์๋ฃ๋ ์์ ์ ์ด๋ป๊ฒ ์ ์ ์์๊น?
์คํ๋ง์ ์์กด๊ด๊ณ ์ฃผ์
์ด ์๋ฃ๋๋ฉด ์คํ๋ง ๋น์๊ฒ ์ฝ๋ฐฑ ๋ฉ์๋๋ฅผ ํตํด์ ์ด๊ธฐํ ์์ ์ ์๋ ค์ฃผ๋ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
โญ๏ธ์คํ๋ง ๋น์ ์ด๋ฒคํธ ๋ผ์ดํ์ฌ์ดํดโญ๏ธ
์คํ๋ง์ปจํ
์ด๋์์ฑ -> ์คํ๋ง๋น์์ฑ -> ์์กด๊ด๊ณ์ฃผ์
-> ์ด๊ธฐํ์ฝ๋ฐฑ ์ฌ์ฉ ์๋ฉธ์ ์ฝ๋ฐฑ -> ์คํ๋ง ์ข
๋ฃ
์คํ๋ง์ ํฌ๊ฒ 3๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋น ์๋ช ์ฃผ๊ธฐ ์ฝ๋ฐฑ์ ์ง์ํ๋ค.
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);
}
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 {
connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
@Override
public void destroy() throws Exception {
disConnect();
}
}
InitializingBean ์ afterPropertiesSet() ๋ฉ์๋๋ก ์ด๊ธฐํ๋ฅผ ์ง์ํ๋ค. DisposableBean ์ destroy() ๋ฉ์๋๋ก ์๋ฉธ์ ์ง์ํ๋ค.
ํ ์คํธ ์ฝ๋๋ ๊ทธ๋๋ก ๋์๋ค.
์์ฑ์ ํธ์ถ, url = null
NetworkClient.afterPropertiesSet
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง
13:24:49.043 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing NetworkClient.destroy
close + http://hello-spring.dev
์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์ด๊ธฐํ ๋ฉ์๋๊ฐ ์ฃผ์ ์๋ฃ ํ์ ์ ์ ํ๊ฒ ํธ์ถ๋์๊ณ , ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์ข ๋ฃ๋์ ์๋ฉธ ๋ฉ์๋๊ฐ ํธ์ถ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ด ์ฝ๋๋ ์คํ๋ง ์ ์ฉ ์ธํฐํ์ด์ค์ ์์กดํ๊ธฐ ๋๋ฌธ์ ์ข์ ๋ฐฉ๋ฒ์ด ์๋๋ค.
package hello.core.lifecycle;
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);
}
public void init() {
System.out.println("NetworkClient.init");
connect();
call("์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง");
}
public void close() {
System.out.println("NetworkClient.close");
disConnect();
}
}
ํ ์คํธ ์ฝ๋์์ @Configuration(์ค์ ์ ๋ณด) ๋ถ๋ถ๋ง ๋ฐ๋์๋ค.
์ค์ ์ ๋ณด์ @Bean(initMethod = "init", destroyMethod = "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;
}
}
์์ฑ์ ํธ์ถ, url = null
NetworkClient.init
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง
13:33:10.029 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing NetworkClient.close
close + http://hello-spring.dev
์ด๊ธฐํ ๋ฉ์๋์, ์ข ๋ฃ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํน์ง์ด ์๋ค.
package hello.core.lifecycle;
import javax.annotation.PostConstruct;
import javax.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();
}
}
//ํ
์คํธ ์ฝ๋
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClient networkClient() {
NetworkClient networkClient = new NetworkClient();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
//์ถ๋ ฅ๊ฒฐ๊ณผ
์์ฑ์ ํธ์ถ, url = null
NetworkClient.init
connect: http://hello-spring.dev
call: http://hello-spring.dev message = ์ด๊ธฐํ ์ฐ๊ฒฐ ๋ฉ์์ง
19:40:50.269 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing NetworkClient.close
close + http://hello-spring.dev
๐@PostConstruct, @PreDestroy ์ ๋ ธํ ์ด์ ํน์ง