강의

멘토링

로드맵

Inflearn Community Q&A

skim87004774's profile image
skim87004774

asked

Spring Core Principles - Basic Edition

Bean lifecycle callback start

빈 콜백

Resolved

Written on

·

199

0

 

 

public class BeanLifeCycleTest {
      @Test
      public void lifeCycleTest() {
          ConfigurableApplicationContext ac = new
  AnnotationConfigApplicationContext(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 이 null 값으로 출력되는것이 당연하다느 부분이 잘 이해 되지 않습니다.

스프링 등록하는 @Configuration 에 이미 setUrl 로 "http://~" 로 setUrl이 이미 설정이 되어 있는데 왜 null값이 나오는것 일까요?

  • 아래체 ac.getBean으로 NetworkClient 빈을 가져오면
    1. 구현부애서 networkClient 생성(여기에는 당연히 url이 없음)
    2. 그런데 이후에 setUrl로 url을 설정해줌
    3. 그 결과를 리턴한 것이 결국 NetworkClient Bean 아닌가요?
@Bean
          public NetworkClient networkClient() {
              NetworkClient networkClient = new NetworkClient();
              networkClient.setUrl("http://hello-spring.dev");
              return networkClient;
}
springoop

Answer 1

0

안녕하세요. sy k님, 공식 서포터즈 David입니다.

setUrl이 발생하기 전에 NetworkClient를 생성하는 과정을 한 줄씩 따라가보시겠어요? 그래도 이해가 안되신다면 답글로 재질문 부탁드립니다.

감사합니다.

skim87004774's profile image
skim87004774

asked

Ask a question