注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Java >> 开源技术 >> Spring >> Java正文

servlet中如何访问spring容器

文章来源:中国IT实验室收集整理 作者:佚名 更新时间:2008-6-28 20:35:48 【 】 【加入收藏

 package com.web.action;

 

    import java.util.ArrayList;

    import java.util.List;

 

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

 

    import org.springframework.web.context.WebApplicationContext;

    import org.springframework.web.context.support.WebApplicationContextUtils;

 

    import com.pojo.Products;

    import com.service.IWorksService;

 

    public class Test extends HttpServlet{

          private IWorksService  worksService;

          private WebApplicationContext wac;

          public void init(){

              wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

          }

        protected void doGet(HttpServletRequest req,HttpServletResponse resp){

            System.out.println("servlet ---------------------------------------------------");

            String className=req.getParameter("className");

            List list=new ArrayList();

            if(className!=null&&className.equals("worksService")){

                worksService=(IWorksService)wac.getBean("worksService");

                wac = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 

                try{

                    list=worksService.getWorksList(new Products(), 0);

                    System.out.println("list size"+list.size());

                }catch(Exception e){

                    e.printStackTrace();

                }

 

            }

            try{

                resp.getWriter().append("list size|"+list.size());

                resp.getWriter().flush();

            }catch(Exception e){

                e.printStackTrace();

            }

        }

    }
    IWorksService 为spring中定义的一个服务接口:

    package com.web.action;

 

    import java.util.ArrayList;

    import java.util.List;

 

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

 

    import org.springframework.web.context.WebApplicationContext;

    import org.springframework.web.context.support.WebApplicationContextUtils;

 

    import com.pojo.Products;

    import com.service.IWorksService;

 

    public class Test extends HttpServlet{

       private IWorksService  worksService;

       private WebApplicationContext wac;

          public void init(){

           wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

          }

 

      protected void doGet(HttpServletRequest req,HttpServletResponse resp){

        System.out.println("servlet ---------------------------------------------------");

        String className=req.getParameter("className");

        List list=new ArrayList();

        if(className!=null&&className.equals("worksService")){

         worksService=(IWorksService)wac.getBean("worksService");

         wac = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 

         try{

          list=worksService.getWorksList(new Products(), 0);

          System.out.println("list size"+list.size());

         }catch(Exception e){

          e.printStackTrace();

         }

 

        }

        try{

           resp.getWriter().append("list size|"+list.size());

           resp.getWriter().flush();

        }catch(Exception e){

         e.printStackTrace();

        }

       }

    }
    IWorksService 为spring中定义的一个服务接口:
     <bean id="worksService"
      class="com.service.impl.WorksServiceImpl">
      <property name="worksDao">
       <ref bean="worksDao" />
      </property>
      <property name="divi">
       <ref bean="diviPageUtil" />
      </property>
     </bean>

     <bean id="worksService"
      class="com.service.impl.WorksServiceImpl">
      <property name="worksDao">
       <ref bean="worksDao" />
      </property>
      <property name="divi">
       <ref bean="diviPageUtil" />
      </property>
     </bean>
    可以看出访问sping容器中对象可以通过访问web容器中的固定属性而得到spring容器,进行得到spring容器中的对象

  • 上一篇Java:

  • 下一篇Java:
  • 最 新 热 门
     手机开发平台指南、教程和资料介绍
     关于什么叫面向接口编程
     编写高级JavaScript应用代码
     不要验证,直接转化科学计数法
     Eclipse插件开发中实现刷新和重编译介绍
     Java开源技术:Eclipse的使用技巧详解
     配置eclipse 3.2 使用JDK1.5中文JavaAPI
     集成Windows本地应用到Eclipse RCP 程序中
     hibernate.cfg.xml配置文件的说明
     eclipse开发jface时,main.class解决方法
    最 新 推 荐
     ibatis+spring 集成
     结合Spring2.0和ActiveMQ进行异步消息调用
     servlet中如何访问spring容器
     Spring MVC与struts比较
     Spring security 命名空间的使用
     spring AOP面向切面编程
     关于String对象解析
     关于Spring切点函数@args()
     Spring中事务的传播属性详解
     Spring+Ibatis+事务处理
    相 关 文 章

    Eclipse插件开发中实现刷新和重编译介绍
    Java开源技术:Eclipse的使用技巧详解
    配置eclipse 3.2 使用JDK1.5中文JavaAPI
    集成Windows本地应用到Eclipse RCP 程序中
    hibernate.cfg.xml配置文件的说明
    eclipse开发jface时,main.class解决方法
    eclipse常用&重要的几个快捷键
    Spring security 命名空间的使用
    从Hibernate的映射谈编程思想
    JSF和Hibernate的比较

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用