메서드 지정 방식
public class NetworkClient {
private String url;
public NetworkClient() {
System.out.println("생성자 호출, url = " + url);
}
...
// 메서드 이름을 임의로 변경한다.
public void init() throws Exception {
connect();
call("초기화 연결 메시지");
}
public void close() throws Exception {
disconnect();
}
}public class BeanLifeCycleTest{
@Test
public void lifeCycleTest() {
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(LifeCycleConfig.class);
NetworkClient client = 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;
}
}
}특징
destroyMethod
Last updated