package abu.csdn.servlet;   
import java.io.IOException;   

import javax.servlet.ServletContext;   

import javax.servlet.ServletException;   

import javax.servlet.http.HttpServlet;   

import javax.servlet.http.HttpServletRequest;   

import javax.servlet.http.HttpServletResponse;   

import org.springframework.orm.hibernate3.HibernateTemplate;   

import org.springframework.web.context.WebApplicationContext;   

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

/**  

 * <p>  

 * 演示使用Spring向Servlet注入对象  

 * </p>  

 * User: Abu Date: 2009-7-2 Time: 14:30:55  

 */  

public class CopyOfShowImageServlet extends HttpServlet {   

    HibernateTemplate hibernateTemplate;   

       

       

    /**  

     * <p>  

     * 在Servlet中注入对象的步骤:  

     * 1.取得ServletContext  

     * 2.利用Spring的工具类WebApplicationContextUtils得到WebApplicationContext  

     * 3.WebApplicationContext就是一个BeanFactory,其中就有一个getBean方法  

     * 4.有了这个方法就可像平常一样为所欲为了,哈哈!  

     * </p>  

     */  

    @Override  

    public void init() throws ServletException {           

        super.init();   

                   

        ServletContext servletContext = this.getServletContext();   

                   

        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);   

                   

        hibernateTemplate = (HibernateTemplate)ctx.getBean("hibernateTemplate");   

    }   

    @Override  

    protected void doPost(HttpServletRequest request,   

            HttpServletResponse response) throws ServletException, IOException {   

        doGet(request, response);   

    }   

       

       

    @Override      

    protected void doGet(HttpServletRequest request,   

            HttpServletResponse response) throws ServletException, IOException {   

           

    }   

}

在Servlet中使用spring注入的bean的更多相关文章

  1. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  2. JSP中使用Spring注入的Bean时需要注意的地方

    遇到问题 遇到一个问题:在JSP中,使用Spring注入的Bean对象时,未能正确地获取到想要的对象. 郁闷的是,它也没报错. 研究问题 使用DEBUG功能(好久不在JSP里写Java代码了,都忘了J ...

  3. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  4. 在servlet中使用Spring注入

    修改servlet 的 init 方法,添加以下代码: SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, ...

  5. 如何在servlet中获取spring创建的bean

    package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...

  6. 如何在servlet的监听器中使用spring容器的bean

    另外补充下:在web Server容器中,无论是Servlet,Filter,还是Listener都不是Spring容器管理的,因此我们都无法在这些类中直接使用Spring注解的方式来注入我们需要的对 ...

  7. 如何在线程中获取spring 管理的bean

    转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec spring xml中定义 <!--spring 工具类-- ...

  8. 170630、springboot编程之普通类中调用spring管理的bean对象

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

  9. SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

    分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...

随机推荐

  1. (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)

    刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...

  2. 百度之星资格赛 hdu 4826 Labyrinth 动态规划

    /********************* Problem Description 是一仅仅喜欢探险的熊.一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上 ...

  3. view动画库

    Android View Animations这个项目收集了各种有趣的动画效果. 所有效果: Attension Flash, Pulse, RubberBand, Shake, Swing, Wob ...

  4. jstl经典用法

    jstl的forEach使用和set变量实现自增: <body> <c:set var="index" value="0" /> < ...

  5. mvc自带的异步表单提交

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Set Windows IP by Batch

    netsh interface ip set address name="Local" static 192.168.1.55 255.255.255.0 192.168.1.1 ...

  7. HTML与CSS入门——第五章 使用文本块和列表

    知识点: 1.在页面上对齐文本的方法 2.三种HTML列表的使用方法 3.在列表中放置列表的方法 5.1 在页面上对齐文本: 父元素内子元素文本的居中:在控制父元素的text-align:center ...

  8. iOS基础框架的搭建 / 及国际化操作

    1.基础框架的搭建 1.1 pod引入常用的第三方类库 1.2 创建基础文件夹结构/目录结构 Resource———存放声音/图片/xib/storyboard 等资源文件 Define——宏定义, ...

  9. 有几数组表单,js怎么获得数组并动态相加输出到文本框

    有几数组表单,js如何获得数组并动态相加输出到文本框<input   name= "fee1[] "> <input   name= "fee2[] & ...

  10. C#Graphics画图

    public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { G ...