spring-ioc-clone_v1

정용현·2026년 4월 21일

프로그래머스

목록 보기
1/3

클론용레포

제출PR

요구사항 v1

  • 요구사항 v1 - 리포지터리에서 포크 후 클론
  • build.gradle.kts 에 기술된 라이브러리만 사용할 수 있습니다.
  • ApplicationContextTest.java 의 모든 테스트케이스 통과
  • src/test 폴더 안의 소스코드는 수정할 수 없음
  • 빈 생성시 하드코딩방식을 사용해도 됩니다.

ApplicationContextTest.java

package com.ll.framework.ioc;

import com.ll.domain.testPost.testPost.repository.TestPostRepository;
import com.ll.domain.testPost.testPost.service.TestFacadePostService;
import com.ll.domain.testPost.testPost.service.TestPostService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ApplicationContextTest {
    private static ApplicationContext applicationContext;

    @BeforeAll
    public static void beforeAll() {
        applicationContext = new ApplicationContext();
    }

    @Test
    @DisplayName("ApplicationContext 객체 생성")
    public void t1() {
        System.out.println(applicationContext);
    }

    @Test
    @DisplayName("testPostService 빈 얻기")
    public void t2() {
        TestPostService testPostService = applicationContext
                .genBean("testPostService");

        assertThat(testPostService).isNotNull();
    }

    @Test
    @DisplayName("testPostService 빈을 다시 얻기, 싱글톤이어야 함")
    public void t3() {
        TestPostService testPostService1 = applicationContext
                .genBean("testPostService");

        TestPostService testPostService2 = applicationContext
                .genBean("testPostService");

        assertThat(testPostService1).isNotNull();
        assertThat(testPostService2).isNotNull();
        assertThat(testPostService1).isSameAs(testPostService2);
    }

    @Test
    @DisplayName("testPostRepository")
    public void t4() {
        TestPostRepository testPostRepository = applicationContext
                .genBean("testPostRepository");

        assertThat(testPostRepository).isNotNull();
    }

    @Test
    @DisplayName("testPostService has testPostRepository")
    public void t5() {
        TestPostService testPostService = applicationContext
                .genBean("testPostService");

        assertThat(testPostService).hasFieldOrPropertyWithValue(
                "testPostRepository",
                applicationContext.genBean("testPostRepository")
        );
    }

    @Test
    @DisplayName("testFacadePostService has testPostService, testPostRepository")
    public void t6() {
        TestFacadePostService testFacadePostService = applicationContext
                .genBean("testFacadePostService");

        assertThat(testFacadePostService).hasFieldOrPropertyWithValue(
                "testPostService",
                applicationContext.genBean("testPostService")
        );

        assertThat(testFacadePostService).hasFieldOrPropertyWithValue(
                "testPostRepository",
                applicationContext.genBean("testPostRepository")
        );
    }
}

ApplicationContext

public class ApplicationContext {

    private final TestPostService testPostService;
    private final TestPostRepository testPostRepository;
    private final TestFacadePostService  testFacadePostService;

    private Map<String,Object> beans = new HashMap<>();

    public ApplicationContext() {
        testPostRepository = new TestPostRepository();
        testPostService = new TestPostService(testPostRepository);
        testFacadePostService = new TestFacadePostService(testPostService,testPostRepository);

        beans.put("testPostRepository", testPostRepository);
        beans.put("testPostService", testPostService);
        beans.put("testFacadePostService", testFacadePostService);
    }

    public <T> T genBean(String beanName) {
        return (T) beans.get(beanName);
    }
}
  • 하드코딩 해도 된다길래 전부 map에다가 때려 박았다.
    문제는 빈의 개수가 늘어나면 그대로 계속 new 해줘야 하고
    또 의존성 주입 순서도 신경써줘야한다.
profile
청년치매 예방기

0개의 댓글