spring core : spring bean life cycle

This commit is contained in:
haerong22
2021-10-08 00:05:31 +09:00
parent 46f6fb5596
commit 6cccf71342
4 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package com.example.basic.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;
class BeanLifeCycleTest {
@Test
public void lifeCycleTest() {
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClientInterface bean = ac.getBean(NetworkClientInterface.class);
NetworkClientMethod bean1 = ac.getBean(NetworkClientMethod.class);
NetworkClientAnnotation bean2 = ac.getBean(NetworkClientAnnotation.class);
ac.close();
}
@Configuration
static class LifeCycleConfig {
@Bean
public NetworkClientInterface networkClientInterface() {
NetworkClientInterface networkClient = new NetworkClientInterface();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
@Bean(initMethod = "init", destroyMethod = "close")
public NetworkClientMethod networkClientMethod() {
NetworkClientMethod networkClient = new NetworkClientMethod();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
@Bean
public NetworkClientAnnotation networkClientAnnotation() {
NetworkClientAnnotation networkClient = new NetworkClientAnnotation();
networkClient.setUrl("http://hello-spring.dev");
return networkClient;
}
}
}

View File

@@ -0,0 +1,44 @@
package com.example.basic.lifecycle;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class NetworkClientAnnotation {
private String url;
public NetworkClientAnnotation() {
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() throws Exception {
System.out.println("NetworkClientAnnotation.init");
connect();
call("초기화 연결 메시지");
}
@PreDestroy
public void close() throws Exception {
System.out.println("NetworkClientAnnotation.close");
disconnect();
}
}

View File

@@ -0,0 +1,44 @@
package com.example.basic.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class NetworkClientInterface implements InitializingBean, DisposableBean {
private String url;
public NetworkClientInterface() {
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 {
System.out.println("NetworkClientInterface.afterPropertiesSet");
connect();
call("초기화 연결 메시지");
}
@Override
public void destroy() throws Exception {
System.out.println("NetworkClientInterface.destroy");
disconnect();
}
}

View File

@@ -0,0 +1,39 @@
package com.example.basic.lifecycle;
public class NetworkClientMethod {
private String url;
public NetworkClientMethod() {
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() throws Exception {
System.out.println("NetworkClientMethod.init");
connect();
call("초기화 연결 메시지");
}
public void close() throws Exception {
System.out.println("NetworkClientMethod.close");
disconnect();
}
}