Web/Spring 2018. 9. 2. 11:32

1.StaticApplicationContext

-코드를 통해 빈 메티정보를 등록하기 위해 사용한다. 거의 사용되지 않는 구현체이다.


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
31
32
        //단순 빈을 등록하고 불러오는 작업
        StaticApplicationContext context=new StaticApplicationContext();
        context.registerSingleton("hello", Hello.class);
        
        Hello hello=(Hello) context.getBean("hello");
        hello.setName("yeoseong_yoon");
        System.out.println(hello.getName());
        
        //빈등록전에 해당 오브젝트에 대한 사용자정의 후 빈등록
        BeanDefinition bd=new RootBeanDefinition(Hello.class);
        bd.getPropertyValues().addPropertyValue("name","sora_hwang");
        context.registerBeanDefinition("hello2", bd);
        Hello hello2=(Hello) context.getBean("hello2");
        System.out.println(hello2.getName());
        
        //컨테이너에 등록된 빈의 개수를 가져온다
        System.out.println("빈등록개수:"+context.getBeanFactory().getBeanDefinitionCount());*/
        
        /*//빈의 관계설정을 하는 예제
        StaticApplicationContext context=new StaticApplicationContext();
        //StringBuffered type의 빈 등록
        context.registerBeanDefinition("printer"new RootBeanDefinition(StringBuffered.class));
        
        BeanDefinition helloDef=new RootBeanDefinition(Hello.class);
        helloDef.getPropertyValues().addPropertyValue("name","Spring");
        helloDef.getPropertyValues().addPropertyValue("printer",new RuntimeBeanReference("printer"));
        
        context.registerBeanDefinition("hello", helloDef);
        
        Hello hello=context.getBean("hello",Hello.class);
        hello.print();
        System.out.println(context.getBean("printer").toString());
cs




2.GenericApplicationContext

-실전에서 사용할 수 있을 정도로 왠만한 컨테이너기능을 다 가진 녀석이다. 빈 설정을 불러오기 위해 reader를 이용해야 한다.

-코드 레벨에서 직접이용하는 경우는 드물지만 우리는 테스트를 위한 프레임워크인 JUnit을 사용할 때 종종 사용하곤 한다.


1
2
3
4
5
6
7
8
9
10
        //xml설정파일 로딩
        GenericApplicationContext gac=new GenericApplicationContext();
        XmlBeanDefinitionReader xbdr=new XmlBeanDefinitionReader(gac);
        xbdr.loadBeanDefinitions("classpath:root-context.xml");
        //설정파일을 읽어왔으니 컨테이너 초기화
        gac.refresh();
        Hello hello = gac.getBean("hello",Hello.class);
        hello.print();
        System.out.println(gac.getBean("printer",StringBuffered.class));
        
cs


1
2
3
4
5
6
7
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext.xml")
public class ApplicationContextTest {
    
    @Autowired
    ApplicationContext context;
}
cs


=>자동 빈 주입되는 ApplicationContext의 구현체가 GenericApplicationContext 녀석이다.



3.GenericXmlApplicationContext

-GenericApplicationContext는 reader까지 생성해서 설정파일을 읽어오는 코드까지 사용자가 직접 작성해야 한다. 그래서 나온 녀석이 이녀석이다.

-이 구현체에는 reader까지 포함된 녀석이다. xml을 이용하는 루트 컨텍스트를 만들 때만 사용할 수 있게 만든 편리한 클래스이다. 세밀한 컨텍스트 설정을 위해서는 GenericApplicationContext를 이용해야한다.


1
2
3
4
GenericXmlApplicationContext context=new GenericXmlApplicationContext("classpath:applicationContext.xml");
 
A aClass=context.getBean("aClass");
.....
cs




4.WebApplicationContext

-ApplicationContext를 확장한 인터페이스이다. 즉, 이 인터페이스를 구현한 클래스를 사용하게 되는 것이다. 이름 그대로 웹 환경에서 사용할 때 필요한 기능이 추가된 애플리케이션 컨텍스트이다.

-XmlWebApplicationContext, AnnotationConfigWebApplication 등을 사용하며, 디폴트는 XmlWebApplicationContext이다.


스프링의 IOC가 적용된 애플리케이션을 작동시키는 방법은 반드시 초기화된 ApplicationContext의 메소드를 호출함으로써 기동되는 것이다. 위의 예제들에서 getBean이라는 메소드를 호출함으로써 기능들이 시작되듯이 말이다. 하지만 WebApplicationContext 처럼 웹환경에서 애플리케이션을 작동시키는 방법은 조금다르다. 웹환경에서는 main()를 호출하기 쉽지 않기다. 이러한 역할을 하는 녀석이 스프링에서 제공하는 DispatcherServlet이라는 녀석이다.


디스패처서블릿은 미리 초기화된 ApplicationContext에 대한 빈정보를 하나 가져온다. 그리고 클라이언트의 요청이 있을 때, 미리 받아온 빈정보를 이용하여 메소드를 호출하고, 그러므로써 애플리케이션이 실행되는 것이다.
















posted by 여성게
: