inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

인터페이스 InitializingBean, DisposableBean

강의대로 실행이 안돼요.ㅜ

해결된 질문

425

최성규

작성한 질문수 25

3

hello.core.lifecycle.NetworkClient.java

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);
        /*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);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        /* 생성자에 있던 코드를 이곳으로 ---> "생성과 초기화를 분리" */
        System.out.println("NetworkClient.afterPropertiesSet");
        connect();
        call("초기화 연결 메시지");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("NetworkClient.destroy");
        System.out.println("destroy() : url = " + url);
        disconnect();
    }
}


hello.core.lifecycle.BeanLifeCycleTest.java

package hello.core.lifecycle;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
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 = new AnnotationConfigApplicationContext(NetworkClient.class);
        NetworkClient client = ac.getBean(NetworkClient.class);
        ac.close();
    }

    @Configuration
    static class LifeCycleConfig {

        @Bean
        public NetworkClient networkClient() {
            NetworkClient networkClient = new NetworkClient();
            networkClient.setUrl("http://hello-spring.net");
            return networkClient;
        }
    }
}

제 코드의 실행 결과

생성자 호출, url = null
NetworkClient.afterPropertiesSet
Connect: null
call: null, message = 초기화 연결 메시지
01:08:27.660 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4a7f959b, started on Fri Dec 11 01:08:27 KST 2020
NetworkClient.destroy
destroy() : url = null
Close: null

oop spring

답변 4

1

최성규

자답입니다. 오타를 찾아서 해결했습니다. 오타는 항상 어처구니가 없네요.

@Test public void lifeCycleTest() 메소드 첫 줄에 LifeCycleConfig.class 여야 하는데, NetworkClient.class 로 적은 부분입니다. 

영한님의 소중한 시간을 위해, 답변 달지 않으셔도 됩니다.

감사합니다.

0

하나

같은 실수때문에 고생하다가 해결하고 갑니다.

감사합니다

0

dev

저도 같은 실수를 해서 질문 남기러 왔다가 깨닫고 갑니다 :)

0

김영한

성규님 스스로 잘 해결하셨습니다^^

이렇게 하는 과정속에서 디버깅 하는 방법을 배울 수 있습니다. 그래서 저는 이런 과정도 중요하다 생각합니다.

화이팅!

구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?

0

49

2

MemberService의 인터페이스를 왜 사용하는지 궁금합니다.

0

72

1

롬복 @Setter를 써야 하는 상황이 있는건가요?

0

87

1

빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?

0

79

1

테스트 속도가 나중에 영향이 있을까요?

0

75

1

gradle 설정 안떠서 질문 남깁니다!

0

116

2

build.gradle로 프로젝트를 여는 이유

0

81

1

provider 사용하는 이유

0

85

1

다음 강의 뭘 들어야 할까요

0

123

2

프로토타입 빈, 직접 destroy 호출 안 할 경우

0

62

1

beanB

0

79

2

퀴즈다시풀기

0

63

1

Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ

0

90

2

"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.

0

63

3

run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>

0

102

2

도메인의 정의?

0

57

1

ApplicationContext 질문입니다.

0

60

1

@Scope의 proxyMode를 사용할때 단위 테스트 방법

0

86

2

ai api 선정하기 관련 질문

0

115

2

생성자 자동주입 관련해서

0

60

1

생성자 직접 호출 vs 팩토리 메서드 패턴

0

93

2

Spring에서 SessionScope와 RequestScope는 함께 사용되나요?

1

63

1

12:25

0

75

2

appConfig.xml 오류

0

127

1