Spring ApplicationContext within Servlet

posted on 03 Dec 2008 09:59 by sonicneo  in Spring

ถ้าต้องการจะจัดการกับ Bean ของ  Spring ซึ่งอยู่ใน Servlet Controller และไม่รู้ว่าจะ implementation ApplicationContext อย่างไร   วันนี้มาแนะนำการใช้ XmlWebApplicationContext ซึ่งง่ายมากๆเขียนแค่ 4 บรรทัด  

XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setServletContext(getServletContext());
ctx.setConfigLocations(new String[] { beans definition locations });
ctx.refresh();

 

Beans definition location จะต้องเป็น relative path เช่น  WEB-INF/META-INF/services/descriptor.xml

และสุดท้ายอย่าลืมเรียนก method refresh() ด้วยนะครับ

Comment



smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry

Tweet

ดีๆๆครับ
เดี๋ยวลองไปใช้มั่งครับ

#1 By lucifer (202.12.118.61) on 2009-10-19 15:49