本文简单编写一个servlet来获取spring容器中管理的<bean  id="dateBean" class="java.util.Date" singleton="false"/>本文只是简单举例,当然可以获取任意你想获取的bean

lib中放入spring所必须的jar包

1.首先看web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>time</servlet-name>
<servlet-class> com.ssh.action.TimeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>time</servlet-name>
<url-pattern>/time.do</url-pattern>
</servlet-mapping>
</web-app>

2.看beans-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dateBean" class="java.util.Date" singleton="false"/>
</beans>

3.TimeServlet.java

package com.ssh.action;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
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; public class TimeServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
WebApplicationContext ctx=WebApplicationContextUtils.
getRequiredWebApplicationContext(this.getServletContext());
PrintWriter out=resp.getWriter();
out.println(ctx.getBean("dateBean"));
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
} }

4.效果图:

java web中如何获取spring容器中定义的bean----WebApplicationContext的使用的更多相关文章

  1. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  2. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  3. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

  4. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  5. [十]SpringBoot 之 普通类获取Spring容器中的bean

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

  6. Spring Boot中普通类获取Spring容器中的Bean

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

  7. 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

    ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:   即是说,当一个类实现了这个接口之 ...

  8. 普通类中获取spring容器中的javabean对象

    spring提供了一系列的*Aware 接口,用于获取相应的对象,进行一系列的对象设置操作,此处实现ApplicationContextAware来获取ApplicationContext. 其他Aw ...

  9. SpringBoot之普通类获取Spring容器中的bean

    package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...

随机推荐

  1. 本地管理表空间(LMT)与自动段空间管理(ASSM)概念

    创建表空间时,extent management local 定义本地管理表空间(LMT),segment space management auto 定义自动段空间管理(ASSM). extent ...

  2. IDEA kotlin 配置

    修改 idea 安装目录 bin 目录 下 idea.properties   文件修改idea.max.intellisense.filesize=50000 避免proto 生成的java文件不被 ...

  3. MySql ERROR 1045 (28000): Access denied 错误

    [MySql ERROR 1045 (28000): Access denied 错误] 即登陆错误,使用MySql登陆时,需使正确的username/passwd.通过 -u -p 参数即可. My ...

  4. linux用户和组2

    1.用户相关命令 su 切换用户 root -> 普通用户 不需要密码 普通用户之间 需要密码 普通用户->root 需要密码 newgrp 切换组身份 whoami 查看当前登录名称 i ...

  5. Python Socket 编程详细介绍(转)

    Python 提供了两个基本的 socket 模块: Socket 它提供了标准的BSD Socket API. SocketServer 它提供了服务器重心,可以简化网络服务器的开发. 下面讲解下 ...

  6. linux下nginx tomcat集群

    集群系统一般通过两台或多台节点服务器系统通过相应的硬件及软件互连,每个群集节点都是运行其自己进程的独立服务器. 这些进程可以彼此通信,对网络客户机来说就像是形成了一个单一系统,协同起来向用户提供应用程 ...

  7. dedecms连表查询参照

    ixingmeib2c/ds/entity_clas/tc_coupon_index.ls.php下面的getIndexInfo()方法

  8. hg 添加用户

    .hg目录下hgrc文件 [ui] username = lyd

  9. Netty 源码(一)服务端启动

    Netty 源码(一)服务端启动 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) ServerBootstap 创建时序图如 ...

  10. [转]微信公众平台(测试接口)开发前的准备工作(转载自walkingmanc的专栏)

    本文转自:http://blog.csdn.net/jiangweicpu/article/details/21228949 http://blog.csdn.net/walkingmanc/arti ...