网上看了很多关于获取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大方法,总有一款适合你。

最新文章

  1. How do I list the files in a directory?
  2. git在分支上创建目录和文件
  3. TOGAF培训讲义
  4. hadoop的关键进程
  5. 通达OA 指纹考勤机接口 源代码
  6. mediastream2使用指南(转载)
  7. ext3文件系统基础
  8. QT5 TK1 串口通信
  9. 几个关于JPEGLIB库的博客
  10. mac在变化mysql-rootpassword-各种解决问题的能力
  11. PHP图片处理之图片背景、画布操作
  12. 利用alias在Linux下设置命令别名
  13. 同样是IT培训,为什么人家月薪过万,你才几千,问题在哪?!
  14. springBoot中使用定时任务
  15. 【转】Docker部署Tomcat及Web应用
  16. 第一篇 Spring boot 配置文件笔记
  17. 深入springboot原理——动手封装一个starter
  18. BlockingQueue 解析
  19. Linux命令大全完整版
  20. A_Pancers团队项目设计完善&amp;编码测试

热门文章

  1. 接近8000字的Spring/SpringBoot常用注解总结!安排!
  2. 【Linux网络基础】上网原理流程
  3. Mac自带编码转换工具iconv
  4. 剑指offer---05---用栈实现队列
  5. WeChatSampleBuilder V2.0 使用教程(网页版+桌面版)
  6. Centos7网络配置( 网关、dns、ip地址配置)
  7. USACO Training Section 1.1黑色星期五Friday the Thirteenth
  8. FMT/FWT学习笔记
  9. SSM整合案例:图书管理系统
  10. Spring杂谈 | 你真的了解泛型吗?从java的Type到Spring的ResolvableType