inflearn logo
講義

講義

知識共有

vkdlxj3562さんの投稿

vkdlxj3562 vkdlxj3562

@charliezip

レビュー投稿数
-
平均評価
-

投稿 2

Q&A

빈 이름 설정 관련 질문

혹시 도움이 되실까 적어봅니다. 영한님의 스프링 핵심원리 강의를 참고하여 빈 이름을 조회해 보았습니다. 먼저 빈 이름 조회하는 테스트 코드입니다. package hello.thymeleaf.basic; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import static org.junit.jupiter.api.Assertions.*; class BasicControllerTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(BasicController.HelloBean.class); @Test @DisplayName("애플리케이션 빈 출력하기") void findApplicationBean() { String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName); //Role ROLE_APPLICATION: 직접 등록한 애플리케이션 빈 //Role ROLE_INFRASTRUCTURE: 스프링이 내부에서 사용하는 빈 if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) { Object bean = ac.getBean(beanDefinitionName); System.out.println("name = " + beanDefinitionName + " object=" + bean); } } } } 첫번째로, @Component("helloBean") 빈 이름을 지정해 준 경우를 테스트 해보겠습니다. @Component("helloBean") static class HelloBean { public String hello(String data) { return "Hello " + data; } } 이 경우에는 빈 이름이 "helloBean" 으로 잘 등록되어 있는게 확인 됩니다. 두번째로, @Component 로 빈 이름을 지정해주지 않은 경우를 테스트 해보겠습니다. @Component static class HelloBean { public String hello(String data) { return "Hello " + data; } } 이 경우에는 빈 이름이 "basicController.HelloBean" 으로 등록되어 있습니다. 그래서 혹시 타임리프가 basicController.HelloBean 빈 이름을 인식할까 해보았지만 인식하지 않더라고요 ㅠ 부족하지만 도움이 되셨으면 좋겠습니다.

いいね数
2
コメント数
5
閲覧数
533