网上看了很多关于获取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. openssl查看证书细节 [转载]

    openssl x509部分命令 打印出证书的内容: openssl x509 -in cert.pem -noout -text 打印出证书的系列号 openssl x509 -in cert.pe ...

  2. [redis]SDS和链表

    一.SDS 1.SDS结构体 redis3.2之前:不管buf的字节数有多少,都用 4字节的len来储存长度,对于只存短字符串那么优点浪费空间,比如只存 name,则len=4 则只需要一个字节8位即 ...

  3. Spring Boot的exit code

    文章目录 Spring Boot的exit code 自定义Exit Codes ExitCodeGenerator ExitCodeExceptionMapper ExitCodeEvent Spr ...

  4. SQL之常用函数

    表8-2 中的SOUNDEX 需要做进一步的解释.SOUNDEX 是一个将任何文本串转换为描述其语音表示的字母数字模式的算法.SOUNDEX 考虑了类似的发音字符和音节,使得能对字符串进行发音比较而不 ...

  5. 5.Python是怎么解释的?

    Python是怎么解释的? Python language is an interpreted language. Python program runs directly from the sour ...

  6. vue2.0学习笔记(第八讲)(vue-cli的使用)

    vue-cli相当于脚手架,可以帮助我们自动生成模板工程.其内部集成了很多的项目模板,如simple.webpack.webpack-simple等.其中webpack这个项目模板适用于大型项目的开发 ...

  7. NodeJS实现websocket代理机制

    使用的模块 ws http http-proxy 主要通过htt-proxy实现中转 启动websocket服务 var WebSocketServer = require('ws').Server; ...

  8. nginx经验分享

    如果我们在使用启动nginx时,遇到这样的提示: nginx: [alert] could not open error log file: open() "/usr/local/var/l ...

  9. P4430 小猴打架、P4981 父子

    prufer编码 当然你也可以理解为 Cayley 公式,其实这个公式就是prufer编码经过一步就能推出的 P4430 小猴打架 P4981 父子 这俩题差不多 先说父子,很显然题目就是让你求\(n ...

  10. 概率dp部分题目

    记录一些比较水不值得单独写一篇blog的概率dp题目 bzoj3036 绿豆蛙的归宿 Description 随着新版百度空间的下线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 给出一个有向 ...