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. [Angular 2] @ViewChild to access Child component's method

    When you want to access child component's method, you can use @ViewChild in the parent: Parent Compo ...

  2. arclistsg独立单表模型文档列表

    arclistsg独立单表模型文档列表 (DedeCMS > 5.3) 名称:arclistsg 功能:类似arclist标签,获取指定单表模型(例如:分类信息),指定栏目,指定排序及呈现样式的 ...

  3. 【nodejs学习】2.网络相关

    1.官方文档的一个小例子 //http是内置模块 var http = require('http'); http.createServer(function(request, response){ ...

  4. static静态类与非静态类的区别

    static静态类与非静态类的区别 1.在非静态类中可以有实例成员也可以有静态成员 2.在调用的时候需要使用对像名.实例成员调用(先要实例化,如person ps=new person();  ps. ...

  5. QML学习心得

    Qt Quick之于QML,正如Qt 之于 C++,QML是Qt中开发的一个新的语言,而Qt Quick是这个语言的一个组件库,其中包含了很多用QML写的可以现成使用的组件. QML Hello Wo ...

  6. phpcms v9升级后台无法上传缩略图的原因分析

    phpcms V9 是目前国内使用人数最多的一款开源免费的CMS系统,正是由于他的免费性,开源性,以及其自身的功能性比较强大,所以倍受许多站长朋友们的亲来,以及许多的公司的喜欢.phpcms也为了完善 ...

  7. 面试题,自己写写dome总是好的

    1.完成如下图所示的布局 <html> <head> <meta http-equiv="Content-Type" content="te ...

  8. C语言基础学习基本数据类型-int类型与int变量

    int类型与int变量 针对不同的用途,C语言提供了多种整数类型.各种整数类型的区别在于所提供数值的范围,以及数值是否可以取负值. 在之前的实例中你已经看到过,int关键字用于声明整型变量. int类 ...

  9. instanceof操作符判断对象类型

    instanceof 的语法格式如下: myobject instanceof ExampleClass myobject:某类的对象引用 ExampleClass:某个类 class Quadran ...

  10. Jasper_crosstab_group _Error incrementing crosstab dataset

    error detail: net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeExceptio ...