본문 바로가기
Spring

spring bean 객체 얻어오기

by 자바초보자 2015. 10. 7.
728x90

spring bean 객체 얻어오기

control에서 bean객체 얻어오기
1. web.xml셋팅
<servlet-mapping><servlet-name>test2</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
2.
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
ServletContext conext = session.getServletContext();
WebApplicationContext wContext = WebApplicationContextUtils.getWebApplicationContext(conext);
mybean sBean = (mybean)wContext.getBean("sampleBean");
or
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
mybean sBean = (mybean) ctx.getBean("sampleBean");
728x90

'Spring' 카테고리의 다른 글

spring 2.5 task scheduler  (0) 2015.10.07
[Spring] AOP를 이용한 속도 측정  (0) 2015.10.07
Spring 다국어 변경  (0) 2015.10.07
Spring schedule  (0) 2015.10.07
Spring ExceptionResolver  (0) 2015.10.07