설정 정보에 @Bean(InitMethod="init" , destroyMethod="close")처럼 초기화 , 소멸 메소드를 지정할 수 있다.
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() throws Exception { connect(); call("초기화 연결 메시지"); } // 종료될 때 public void close() throws Exception { System.out.println("destory"); disconnect(); } }
public class BeanLifeCycleTest { @Test public void lifeCycleTest(){ // 스프링 컨테이너 생성 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class); NetworkClient bean = ac.getBean(NetworkClient.class); // 스프링 컨테이너 종료 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; } } }
생성자 호출 , url = null Connect : http://hello-spring.dev Call : http://hello-spring.dev message : 초기화 연결 메시지 destory close : http://hello-spring.dev
스프링 핵심 원리 - 기본편 - 인프런
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 초급 프레임워크 및 라이브러리 웹 개발 서버 개발 Back-End Spring 객체지향 온
www.inflearn.com
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.
빈 등록 초기화 , 소멸 메소드
설정 정보에 @Bean(InitMethod="init" , destroyMethod="close")처럼 초기화 , 소멸 메소드를 지정할 수 있다.
Class
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() throws Exception { connect(); call("초기화 연결 메시지"); } // 종료될 때 public void close() throws Exception { System.out.println("destory"); disconnect(); } }
Test
public class BeanLifeCycleTest { @Test public void lifeCycleTest(){ // 스프링 컨테이너 생성 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class); NetworkClient bean = ac.getBean(NetworkClient.class); // 스프링 컨테이너 종료 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; } } }
출력
설정 정보 사용 특징
종료 메소드 추론
스프링 핵심 원리 - 기본편 - 인프런
스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 초급 프레임워크 및 라이브러리 웹 개발 서버 개발 Back-End Spring 객체지향 온
www.inflearn.com
'스프링 핵심 원리 > 빈 생명주기 콜백' 카테고리의 다른 글