前几天写web项目的时候,用到了spring mvc。

但是又写bean。我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的。

所以,只能通过ApplicationContext来获取。

在servlet里面获取ApplicationContext其实可以通过spring提供的方法:

1
WebApplicationContextUtils.getWebApplicationContext(ServletContext)

来获取。

这个方法前提是要在web.xml里面即一个listener:

1
2
3
4
5
6
7
8
9
10
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-servlet.xml,
            classpath:applicationContext.xml
        </param-value>
    </context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

不过这样子只能在servlet里面获取。

如果要在servlet外获取ApplicationContext呢?

其实可以封装一下的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package me.idashu.code.util;
 
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
/**
 * 描述:ApplicationContext容器
 *
 * @author: dashu
 * @since: 13-5-11
 */
public class AppContext implements ServletContextListener {
 
    private static WebApplicationContext springContext;
 
    public AppContext() {
        super();
    }
 
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
 
    }
 
 
    public void contextDestroyed(ServletContextEvent event) {
    }
 
    public static ApplicationContext getApplicationContext() {
        return springContext;
    }
 
}

还需要在web.xml里面再加条记录:

1
2
3
<listener>
        <listener-class>me.idashu.code.util.AppContext</listener-class>
    </listener>

其实就是在content初始化的时候,把ApplicationContext保存起来,下次方便调用。

spring中获取applicationContext(2)的更多相关文章

  1. spring中获取applicationContext

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

  2. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  3. spring mvc在Controller中获取ApplicationContext

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

  4. spring中获取dao或对象中方法的实例化对象

    spring中获取dao的中方法的实例化对象: //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quo ...

  5. Spring中获取被代理的对象

    目录 Spring中获取被代理的对象 获取Spring被代理对象什么时候可能会用到? Spring中获取被代理的对象 Spring中获取被代理的对象 ### 获取Spring被代理对象的JAVA工具类 ...

  6. spring中获取当前项目的真实路径

    总结: 方法1: WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext ...

  7. spring 代码中获取ApplicationContext(@AutoWired,ApplicationListener)

    2017年度全网原创IT博主评选活动投票:http://www.itbang.me/goVote/234    学习spring框架时间不长,一点一滴都得亲力亲为.今天忽然觉得老是通过@Autowir ...

  8. Spring中获取对象

    Spring是一个非常主流,而且是好用的框架.提供管理对象的容器,提供事务的支持,缓存,权限认证(往往不用).用来集成hibernate等.而管理对象的生命周期是其中一个非常重要的功能.在Spring ...

  9. Spring +quartz获取ApplicationContext上下文

    job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...

随机推荐

  1. Android-Xml,PullParser,解析

    在上一篇博客,Android-XML格式描述,介绍来XML在Android中的格式: 解析persons.xml文件内容: <?xml version="1.0" encod ...

  2. Java核心编程快速学习(转载)

    http://www.cnblogs.com/wanliwang01/p/java_core.html Java核心编程部分的基础学习内容就不一一介绍了,本文的重点是JAVA中相对复杂的一些概念,主体 ...

  3. ajax C# webapi上传图片

    html ajax上传图片到服务器 后端采用asp.net webapi 前端有各种现实上传图片的控件,样式可以做的很美观.我这里只用基本的样式做图片上传. 前端代码 <input name=& ...

  4. 使用ABP框架踩过的坑系列2

    ABP中有很多惯例,如果使用得当,可以事半功倍,如果使用不当,也会有很大的麻烦,是否适当其实还是要看Need需求 ASP.NET Boilerplate (ABP) is an open source ...

  5. 什么是fortran语言之fortran语言入门

    Fortran源自于“公式翻译”(英语:FormulaTranslation)的缩写,是一种编程语言.它是世界上最早出现的计算机高级程序设计语言,广泛应用于科学和工程计算领域.FORTRAN语言以其特 ...

  6. influxDB 基本操作

    #创建数据库 create database "db_name"   #显示所有的数据库 show databases   #删除数据库 drop database "d ...

  7. OSX - 可以安装任何程序!

    在shell里面执行命令: sudo spctl --master-disable 参考: https://www.jianshu.com/p/010cc30228f3

  8. pandas 对dataframe一列中某些值进行处理

    https://github.com/Bifzivkar/Boutique-Travel-Services-Predict/blob/master/feature/5_extract_feature. ...

  9. 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  10. webpack快速入门——CSS进阶:消除未使用的CSS

    使用PurifyCSS可以大大减少CSS冗余 1.安装 cnpm i purifycss-webpack purify-css --save-dev 2.引入glob,因为我们需要同步检查html模板 ...