흐음... 스프링 컨텐이너에서 관리되지 않는 객체에서 스프링 빈을 사용하고 싶을때가 있다고 치고...
이런 경우 스프링이 제공하는 WebApplicationContextUtils 클래스를 이용해서 WebApplicationContext에 접근할 수 있다.

WebApplicationContextUtils 클래스는 DispatcherServlet이나 ContextLoaderListener가 생성한 WebApplicationContext에 접근할 수 있는
static 메소드를 제공한다.

ㄱ. WebApplicationContext getWebApplicationContext(ServletContext sc)
ContextLoaderListener가 생성한 루트 WebApplicationContext를 구한다.
ㄴ. WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
지정한 속성이름 (attrName)으로 저장된 WebApplicationContext를 구한다.
주로 DispatcherServlet이 생성한 WebApplicationContext에 접근할 때 사용된다.

WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object commonLogger = rootContext.getbean("commonLogger");

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

의 경우

org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher 이다.

실제 접근시에

WebApplicationContext dispatcherContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext(), "org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");

Object helloController = dispatcherContext.getBean("helloController");






 

 

'IT > 스프링' 카테고리의 다른 글

뷰 영역???  (0) 2012.01.26
@RequestBody , @ResponseBody  (0) 2012.01.20
캐시옵션 설정  (0) 2012.01.19
HandlerInterceptor  (0) 2012.01.19
파일 업로드 in spring... 3  (1) 2012.01.18

+ Recent posts