网上看了很多关于获取ApplicationContext的方法,五大方法,但是我用web服务使用成功的就这一个,自己记忆下。

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; /**
* 获取ApplicationContext并调用
*/
public class StartListener implements ServletContextListener{
//这是一个类,就是通过xml方式能调用到这个类。必须是静态修饰
private static ItemService itemService; public StartListener(){
super();
} @Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
WebApplicationContext springContext=WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());//获取初始化的上下文
itemService = ( ItemService ) springContext.getBean( "item" );//可以传形参的方式改变想要获取的bean
} /**
* 创建目录,这是调用上面类中的方法
* @param mkdirName
* @return
*/
public String mkdir(String mkdirName){
System.out.println("调用创建目录方法!!");
return itemService.mkdir(mkdirName);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
//销毁
} }

xml中需要的设置:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app id="WebApp_1530018094941">
<display-name>Archetype Created Web Application</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hsf-consumer-beans.xml</param-value>
</context-param>
        //用于监听刚刚配置的类
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.alibaba.edas.carshop.detail.StartListener</listener-class>
</listener>
//下面是接口
<servlet id="Servlet_1531272320787">
<servlet-name>IndexServlet</servlet-name>
<servlet-class>com.alibaba.edas.carshop.detail.IndexServlet</servlet-class>
</servlet>
<servlet-mapping id="ServletMapping_1531272320723">
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/index.htm</url-pattern>
</servlet-mapping> </web-app>

关于获取ApplicationContext的方法,适用于我的是这个方法,其他几个方式你们也可以去试试。总共5大方法,总有一款适合你。

javaWeb普通类获取ApplicationContext的更多相关文章

  1. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContex ...

  2. 普通Java类获取spring 容器的bean的5种方法

    方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...

  3. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  4. 获取applicationContext对象的方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  5. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  6. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  7. spring 框架通过new Object()获取applicationContext 中的bean方案

    工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...

  8. 161018、springMVC中普通类获取注解service方法

    1.新建一个类SpringBeanFactoryUtils 实现 ApplicationContextAware package com.loiot.baqi.utils; import org.sp ...

  9. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

随机推荐

  1. 串口字符串-HEX格式

    串口字符串-HEX格式 C++SerialSerialPortHEX 介绍 串口通信过程中 通常涉及一个数据的模拟过程以及数据发送过程, 一般来说, 我们会发送一串指令给下位机 68 05 00 84 ...

  2. B/S和C/S架构的区别

    一.B/S架构 什么是B/S模式 B/S模式,即浏览器/服务器模式,是一种从传统的二层CS模式发展起来的新的网络结构模式,其本质是三层结构C/S模式.B/S网络结构模式是基于Intranet的需求而出 ...

  3. spark下dataframe转为rdd格式

    dataframe可以实现很多操作,但是存储到本地的时候,只能存 parquest格式 需要存储源格式,需要转换为rdd类型 将dataframe中的每一行都map成有逗号相连的string,就变为了 ...

  4. C++如何求程序运行时间

    C++中常用clock()函数求运行时间,返回值类型为clock_t,返回值是程序运行到本次调用clock()函数经过的clock数,头文件为<time.h>. 用法: 1.求开始时间s= ...

  5. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  6. C# 基础知识系列- 14 IO篇 流的使用

    0. 前言 继续之前的C# IO流,在前几篇小短片中我们大概看了下C# 的基础IO也对文件.目录和路径的操作有了一定的了解.这一篇开始,给大家演示一下流的各种操作.以文件流为例,一起来看看如何操作吧. ...

  7. airtest+poco多脚本、多设备批处理运行测试用例自动生成测试报告

    一:主要内容 框架功能及测试报告效果 airtest安装.环境搭建 框架搭建.框架运行说明 airtest自动化脚本编写注意事项 二:框架功能及测试报告效果 1. 框架功能: 该框架笔者用来作为公司的 ...

  8. OSG程序设计之Hello World 2.0

    现在为Hello World添加一些键盘响应事件. //需要多添加两个库:osgGAd.lib.osgd.lib 代码如下: #include <osgDB/ReadFile> #incl ...

  9. HDU1300Pearls

    传送门 描述: 有几种不同的珍珠.每种珍珠都有它的单价.当然质量高的珍珠价格一定也是高的. 为了避免买家只买1个珍珠.就要求不论是买了多少个珍珠都是需要在购买数量上加10.之后乘上单价. 例如:买5个 ...

  10. P3467(矩形覆盖问题)

    描述:https://www.luogu.com.cn/problem/P3467 1.考虑如果整个建筑物链是等高的,一张高为链高,宽为整个建筑物宽的海报即可完全覆盖: 2.若有两个不等高的元素组成建 ...