熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

servlet中如何訪問spring容器

2013-11-23 20:37:36  來源: Java開源技術 

  package comwebaction;

  import javautilArrayList;

  import javautilList;

  import javaxservlethttpHttpServlet;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import orgsprntextWebApplicationContext;

  import orgsprntextsupportWebApplicationContextUtils;

  import compojoProducts;

  import comserviceIWorksService;

  public class Test extends HttpServlet{

  private IWorksService  worksService;

  private WebApplicationContext wac;

  public void init(){

  wac =WebApplicationContextUtilsgetRequiredWebApplicationContext(thisgetServletContext());

  }

  protected void doGet(HttpServletRequest reqHttpServletResponse resp){

  Systemoutprintln(servlet );

  String className=reqgetParameter(className);

  List list=new ArrayList();

  if(className!=null&&classNameequals(worksService)){

  worksService=(IWorksService)wacgetBean(worksService);

  wac = (WebApplicationContext)thisgetServletContext()getAttribute(WebApplicationContextROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

  try{

  list=worksServicegetWorksList(new Products() );

  Systemoutprintln(list size+listsize());

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  try{

  respgetWriter()append(list size|+listsize());

  respgetWriter()flush();

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  }
    IWorksService 為spring中定義的一個服務接口:

  package comwebaction;

  import javautilArrayList;

  import javautilList;

  import javaxservlethttpHttpServlet;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import orgsprntextWebApplicationContext;

  import orgsprntextsupportWebApplicationContextUtils;

  import compojoProducts;

  import comserviceIWorksService;

  public class Test extends HttpServlet{

  private IWorksService  worksService;

  private WebApplicationContext wac;

  public void init(){

  wac =WebApplicationContextUtilsgetRequiredWebApplicationContext(thisgetServletContext());

  }

  protected void doGet(HttpServletRequest reqHttpServletResponse resp){

  Systemoutprintln(servlet );

  String className=reqgetParameter(className);

  List list=new ArrayList();

  if(className!=null&&classNameequals(worksService)){

  worksService=(IWorksService)wacgetBean(worksService);

  wac = (WebApplicationContext)thisgetServletContext()getAttribute(WebApplicationContextROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

  try{

  list=worksServicegetWorksList(new Products() );

  Systemoutprintln(list size+listsize());

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  try{

  respgetWriter()append(list size|+listsize());

  respgetWriter()flush();

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  }
    IWorksService 為spring中定義的一個服務接口:
     <bean id=worksService
      class=comserviceimplWorksServiceImpl>
      <property name=worksDao>
       <ref bean=worksDao />
      </property>
      <property name=divi>
       <ref bean=diviPageUtil />
      </property>
     </bean>

  <bean id=worksService
      class=comserviceimplWorksServiceImpl>
      <property name=worksDao>
       <ref bean=worksDao />
      </property>
      <property name=divi>
       <ref bean=diviPageUtil />
      </property>
     </bean>
    可以看出訪問sping容器中對象可以通過訪問web容器中的固定屬性而得到spring容器進行得到spring容器中的對象


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28787.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.