티스토리 뷰

Bean은 Spring IoC Container에 의해 관리되는 객체로 객체생성 및 의존주입의 제어권을 개발자가 아닌
Container가 가지고 있다

Spring Container의 대표적인 구현체는 BeanFactory와 ApplicationContext 2가지가 있는데
ApplicationContext로 Spring이 관리하는 Bean목록들을 확인해보자.

 

package com.kjh.study;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudyApplication.class)
public class StudyApplicationTests {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void contextLoads() throws Exception {
        if (applicationContext != null) {
            String[] beans = applicationContext.getBeanDefinitionNames();

            for (String bean : beans) {
                System.out.println("bean : " + bean);
            }
        }
    }
}

 

ApplicationContext의 메소드중 getBeanDefinitionNames() 메서드를 호출하면 Spring Container에
관리되는 Bean (등록된 bean이름) 목록들을 String배열 타입으로 리턴한다.

 

그리고 개발자가 직접만든 Controller, Service 등 각종 Config 파일들도 Spring Container에 관리되는
Bean 객체이기 때문에 아래의 콘솔출력값에 개발자가 직접 작성한 Bean들도 출력되어진다.

 

728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함